- Home /
coroutine not running after yield new WaitForSeconds
Hi everyone,
so I am creating this mobile. Basically its like an endless runner that goes up. So the problem is with this script that I have for this powerup. This powerup basically just increases the speed of the player and all for a short period of time. This is why I had to use a coroutine and yield new waitforSeconds. The problem here is that sometimes it works perfectly fine and sometimes everything that happens after either the first waitforseconds or the second one just does not run. This code used to work fine (yesterday) until today it suddenly just stopped working.
IEnumerator PickupSpeed(Collider2D player)
{
turboParticleEffect = Instantiate(pickupEffect, player.transform.position, transform.rotation);
turboParticleEffect.transform.parent = player.transform;
GetComponent<SpriteRenderer>().enabled = false;
GetComponent<Collider2D>().enabled = false;
player.GetComponent<CircleCollider2D>().enabled = false;
player.GetComponentInChildren<PolygonCollider2D>().enabled = true;
OldMoveSpeed = GM.moveSpeed;
GM.Turbo = true;
UpdateplayerSR(1f, playerBlankColor, "yellow", null);
yield return new WaitForSeconds(duration);
GM.Turbo = false;
Destroy(turboParticleEffect);
yield return new WaitForSeconds(2f);
//UpdateplayerSR(1f, Color.blue, "white", null);
Debug.Log("almost finished turbo");
playerSR.color = playerBlankColor;
Debug.Log("finished turbo");
player.GetComponent<CircleCollider2D>().enabled = true;
Destroy(gameObject);
}
public void UpdateplayerSR(float transitionTime, Color fadeColor, string c, Action func)
{
// playerSR.color = fadeColor;
StartCoroutine(UpdateplayerSRcolor(transitionTime, c, func));
}
private IEnumerator UpdateplayerSRcolor(float transitionTime, string color, Action func)
{
float t = 0.0f;
for (t = 0.0f; t <= 1; t += Time.deltaTime / transitionTime)
{
if (color == "red")
{
//playerSR.color = new Color(t, SR.color.g, SR.color.b);
playerSR.color = Color.Lerp(Color.white, Color.red, t);
}
if (color == "green")
{
//playerSR.color = new Color(0, t, 0);
playerSR.color = Color.Lerp(Color.white, Color.green, t);
}
if (color == "blue")
{
//playerSR.color = new Color(0, 0, t);
playerSR.color = Color.Lerp(Color.white, Color.blue, t);
}
if(color == "white")
{
//playerSR.color = new Color(t, t, t);
playerSR.color = Color.Lerp(Color.red, Color.blue, t);
}
if (color == "yellow")
{
//playerSR.color = new Color(t, t, 0);
playerSR.color = Color.Lerp(Color.white, Color.yellow, t);
}
yield return null;
}
//SR.color.a = 1;
func?.Invoke();
}
}
so I am new with unity and this is my first project. I know, my code is not neat at all and if you have any suggestions on that I would to happy to hear about them too. Would the fact that a coroutine running inside a corountine make it lag?
oh and I have looked at other answers and those that I have seen don't apply to what I am doing and I have tried debugging with debug.log and it seems it was not running if it was after yield return new WaitForSeconds is not running.
Answer by xxmariofer · Apr 15, 2019 at 07:56 AM
thats probably because your object that has the script with the monobehaviour gets destroyed.