Coroutine couldn't be started because the GameObject is inactive

Hello!

I tried to create simple flappy bird game and everything works fine but when i wanted to show the Game Over text with fading (using the lerp and color.a) the “Coroutine couldn’t be started because the GameObject is inactive” error occured and I don’t know why… I tried the activeInHierarchy and it returned false but why? I repeated the process of creating the gameobject but it still did not work and the error occurs still ,here is some code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameSetup : MonoBehaviour {

    private float Lerp = 0;
    public Text gameover_text;
    public GameObject player;
    public GameObject ground;

    void Start () {
        Instantiate (player, new Vector3 (-12, 5, 0), Quaternion.identity);
        Instantiate (ground, new Vector3 (0, -9, 0), Quaternion.identity);
        Instantiate (ground, new Vector3 (0, 9, 0), Quaternion.identity);
    }

    public void GameOver()
    {
        StartCoroutine(Animate());
        Debug.Log("Game Over");
        Debug.Log("CurrentState: " + gameObject.activeInHierarchy);
    }

    private IEnumerator Animate()
    {
        Debug.Log("Coroutine started");
        yield return new WaitForSeconds(0.5f);
        Lerp += 0.1f;
        Fader(Lerp);
        if(gameover_text.color.a != 255)
        {
            StartCoroutine(Animate());
        }
    }

    private void Fader(float lerp)
    {
        gameover_text.color = new Color(0, 0, 0, Mathf.Lerp(0,255,lerp));
        Debug.Log("Current state: " + gameover_text.color.a);
    }
}

More info and code on demand :slight_smile:

Thanks!

It’s probably the parent canvas object that is inactive.

Sorry I did not explain it enough… The script is on ‘GameScript_Handler’ object and that is inactive but when i start the game it generates the player and ground, the generate obstacle script works too (it is on the same object) only the coroutine don’t work :slight_smile:

PS: Sorry for my english, i’m from czech :slight_smile:

Code on Scripts can run, but coroutines can not (or other Unity methods, like Start, Update, etc…)
I would suggest trying by making the game object active. If for whatever reason you always want that object inactive, then you have the option to move the coroutine to some other script/game object… where it’s active :slight_smile:

I want it to be always active … As I said, I tried to create the object again but still it doesn’t work… In editor it says it is active and activeSelf says it too but activeInHiearchy says it is inactive, why? Sorry guys I’m new at unity and I’m still learning it :slight_smile:

I see… well, that means that some parent object is inactive. Which in turns means, for what matters in this topic, that your game object is also active.
You’d have to have whichever parent object(s) active, or as I mentioned before, move the coroutine to some other game object that is … entirely active. :slight_smile:

The only parent object is canvas which is also active but I’m not at home now so i can’t test the activeInHierarchy…

To me, that doesn’t sound possible. If the game object is active, has only 1 active parent… then activeInHierarchy should be true, as well.

I discussed about this with my friend and on another forum they also didn’t know what is it so… :smile: Some Harry Potter things are happening :frowning: :smile:

lol. Sorry we couldn’t work it out. :slight_smile:

That’s ok i will find a way to do this :smile:

The only parent of the gameobject is not canvas it hasn’t any parent at all :smile: Sorry for that mistake :frowning:

If you’re still stuck on this, can you create a unity package (just ignore graphics/sounds) - just the “bare bones” of the game and post the package on the thread, I’d take a look at it for you… :slight_smile:

Here it is: :smile:

3338636–260563–Flappy.unitypackage (52 KB)

Ah, cool will check it out.

Found the problem… Your lerp / fade or whatever code is a bit odd, too. :slight_smile:

I will just tell you the issue, and you can ask about the fade or not, I leave it to you.

So, you were referencing the prefab as the game object with the coroutine. That’s why it was “inactive”.
I wrote a few snippets of code for you:

// in game setup
static GameSetup instance;
void Awake() {
   instance = this;
  }
public static GameSetup GetInstance()
{
   return instance;
}

// in Player Collisions
// erased your old game object GS_ whatever it was called.
public GameSetup gameSetup;

void Start() {
   gameSetup = GameSetup.GetInstance();
  }
// this is your code, slightly modified because of my change.
 private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.otherCollider.tag == "ScoreGate")
        {
            Physics2D.IgnoreCollision(gameObject.GetComponent<Collider2D>(), collision.otherCollider);
        }
        else
        {
                /** I modified this; you could have kept it as a game object, but since you never used
                     it as a game object, and only used it as the type 'GameSetup', I thought this would be better.
                 **/
            gameSetup.GameOver();
        }
    }

Let me know how that goes :slight_smile:

1 Like

It works now no errors but the fader didn’t work can I ask you why? :smile:

Maybe try something like this:

private IEnumerator Animate()
{
   Debug.Log("Coroutine started");
   yield return new WaitForSeconds(0.5f);
    Color origCol = gameover_text.color;
  // this will take ~ 1 second to complete / fade in.
     for(float f = 0; f <= 1; f += Time.deltaTime) {
          origCol.a = f;
          gameover_text.color = origCol;
          yield return null;
         }
     origCol.a = 1;
     gameover_text.color = origCol;
  }

Still no errors, still not working… :smile:

Well, I just tried it and it’s working perfectly for me :slight_smile:

Make sure you have the gameover_text properly referenced. Not sure what else could be wrong.