#Daniyal

1 messages · Page 1 of 1 (latest)

analog wagon
#

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)

astral mist
#

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

analog wagon
#

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

spiral wadi
analog wagon
#
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

spiral wadi
#

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

analog wagon
#

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

astral mist
#

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?

spiral wadi
#

color change

#

4 seconds wait

#

color change

#

keeps on doing it

#

until colision occurs again

astral mist
#

Okay, also, what is the sec timer for?

spiral wadi
#

this is just a game timer which is running in the game for 60 seconds

astral mist
#

Okay

#

I'm honestly not sure here

spiral wadi
analog wagon
#

yes. Because you didn't program it to stop yet. But it is repeating yes?

spiral wadi
analog wagon
#

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
 
        }
    }```
spiral wadi
#

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

analog wagon
#

Throw in a null reference check.

if(routine != null) stop coroutine...

spiral wadi
#

yeah great now working fine but waiting longer than 4 seconds

analog wagon
#

I don't know what's going on there. Put some debug.logs in to see when it refreshes or is cancelled.

spiral wadi
#

And what do you recommend of while loop? because it currently running for 5 iterations

analog wagon
spiral wadi
#

Can I ask you a question regarding how to approach a specific thing in this game? @analog wagon