#Daniyal
1 messages · Page 1 of 1 (latest)
So here is the thing. You're looping inside your Gametimer. I don't really know whats going on there, how is your GameTimer related to you colliding with things?
You need a conditional for your while loop, right now it is while sec!= 0. I don't know what sec is and I don't know when it would hit 0. Or what effects it. Try this
(typing code below)
I'm not sure what the intended behavior is supposed to be
I see that WaitAndRun sets a Trigger, which I guess in turns starts an animation
StopCoroutine will stop the Coroutine itself but not the animation from playing if it has already started, dunno if that's the issue tho
The intended behaviour should be
-Exit collider and start coroutine
-wait 4 seconds, change color
-wait 4 seconds, change color
-wait 4 seconds, change color
.....
-enter collider, stop coroutine
from what I understand
Yeah exactly and should go on to the idle state as well from shake animation
IEnumerator WaitAndRun(){
var i=0;
while(i<5){
yield return new WaitForSeconds(4);
sliderAnim.SetTrigger("toColor");
i++}
}
}
this should make your thing flash 5 times
or your animation play, whatever
OnExitCollision color animation should start after 4 seconds delay and keeps on doing that after every 4 seconds delay than when it collides color animation should stopped and idle it is back to idle animation
basically there is a 60 seconds timer which is running in the game so I put a while loop so coroutine should run until the game is not over and only stops on collision
ignore everything right now. Your problem is the coroutine doesn't loop. lets get it looping before looking at anything else. Walk before we run.
the code I put down should loop. make sure it does
Okay so let me see if I understand, when we EXIT a collision, this coroutine is started, and it should make the object change color every 4 second?
OnCollisionExit
4 seconds wait/
color change
4 seconds wait
color change
keeps on doing it
until colision occurs again
Okay, also, what is the sec timer for?
this is just a game timer which is running in the game for 60 seconds
color animation running and not stopping even after collision it does not stops and go back to idle
yes. Because you didn't program it to stop yet. But it is repeating yes?
Yes it's keeps on running
good, now we just need to kill the coroutine when you run into something. This line is incorrect
if (other.gameObject.tag == "Run")
{
isCollided = true;
sliderAnim.SetTrigger("toIdle");
StopCoroutine(WaitAndRun());
}
``` The problem is, you aren't stopping a coroutine... you're stopping an arbitrary method. You need to assign the started coroutine to a variable, then stop that variable
```cs
Coroutine waitAndRun;///CHANGES HERRE
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Run")
{
isCollided = true;
sliderAnim.SetTrigger("toIdle");
StopCoroutine(waitAndRun); ///CHANGES HERE
}
private void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.tag == "Run")
{
waitAndRun = StartCoroutine(WaitAndRun()); ///CHANGES HERE
}
}```
yeah I have assign thevariable
NullReferenceException: routine is null
UnityEngine.MonoBehaviour.StopCoroutine (UnityEngine.Coroutine routine) (at <028e4d71153d4ed5ac6bee0dfc08aa3b>:0)
MoveRunner.OnCollisionEnter2D (UnityEngine.Collision2D other) (at Assets/Scripts/MoveRunner.cs:48)
but got this error very strange
and animations are not running entirely
it should happen very strange
Throw in a null reference check.
if(routine != null) stop coroutine...
yeah great now working fine but waiting longer than 4 seconds
I don't know what's going on there. Put some debug.logs in to see when it refreshes or is cancelled.
And what do you recommend of while loop? because it currently running for 5 iterations
You can use while(true). that's kind of "dangerous" because it could run forever. It would be smarter to do something like while(object.enabled).
Can I ask you a question regarding how to approach a specific thing in this game? @analog wagon