Coroutine inactivity problem

I am getting an error that says:

Coroutine couldn’t be started because the the game object ‘MainMenuObject’ is inactive!

However, that object is not inactive. It is active in the editor, it is never rendered inactive by my scripts, actually never referenced by them, and the editor itself tells me it’s active, unless there is something that I fundamentally do not understand here.

In the script I have the following 3 lines of code:

  • Debug.Log(GameObject.Find(“MainMenuObject”).activeSelf);
  • Debug.Log(GameObject.Find(“MainMenuObject”).activeInHierarchy);
    • WaitAndLoadLevel(4, 0);

This gives in the console:

  • True
  • UnityEngine.Debug:Log(Object)
  • MainMenuButton:OnClickEscapeYesButton()(at Assets/Scripts/MainMenuButton.js:81)
  • UnityEngine.EventSystems.EventSystem:Update()
  • True
  • UnityEngine.Debug:Log(Object)
  • MainMenuButton:OnClickEscapeYesButton()(at Assets/Scripts/MainMenuButton.js:82)
  • UnityEngine.EventSystems.EventSystem:Update()
  • Coroutine couldn’t be started because the the game object ‘MainMenuObject’ is inactive!
  • UnityEngine.MonoBehaviour:StartCoroutine_Auto(IEnumerator)
  • MainMenuButton:OnClickEscapeYesButton() (at Assets/Scripts/MainMenuButton.js:84)
  • UnityEngine.EventSystems.EventSystem:Update()

I wonder if this error can be caused by something else?

Is there some secret code that is executed between my 3 lines of code that deactivate the MainMenuObject ?

What does your WaitAndLoadLevel function look like? If that’s your coroutine, you need to call the StartCoroutine() method.

Additionally, coroutines only work on monobehaviors, so if you removed the “: Monobehavior” from your class declaration, that would be the problem.

1 Like

I am using javascript, so I never dealt with declaring Monobehavior… should I add some line on top of my script to do so?

Here is the coroutine:

  • function WaitAndLoadLevel (time : float, level : int) {
    • yield WaitForSeconds(time);
    • Application.LoadLevel(level);
    • }

It’s at the end of the same script… should I put it somewhere else?
I read somewhere that in javascript, you do not need to call StartCoroutine() as the compiler does it automatically.

Ahh, I’m sorry, I don’t know how to use coroutines in javascript

In c#, a coroutine must return an IEnumerator, which allows it to keep it’s place in the coroutine. When you return on a yield, the IEnumerator you return has information on which yield you left off on so you can continue there when you continue execution.

I’m wondering if your problem is because you have a “yield” in a function with no IEnumerator? If there is no IEnumerator, there may be something more specific to javascript you need to do that I just don’t know.

I’ll look into that.
Thanks for taking the time to reply!