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

So I have looked at all the questions regarding this error, but none of the answers apply to my case. Honestly, there is something that I must fundamentally not understand here.

I have a script on an object. This object, named “MainMenuObject”, was created for the sole purpose of executing this script, when I push a button (named “EscapeYesButton”). This object is never referenced in any other script. It is active in the editor, and never turned inactive by one of my other scripts.

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()

Here is the full script, the coroutine called is at the end:

#pragma strict

private var gameMaster : GameObject;

private var fader : GameObject;

private var status : int;


function OnClickMainMenuButton () {

	gameMaster = GameObject.Find("GameMaster");

	Debug.Log(gameMaster.GetComponent(MenuingDestroyed).status);
	
	if (gameMaster.GetComponent(MenuingDestroyed).status == 4 || gameMaster.GetComponent(MenuingDestroyed).status == 5){
	
		if (!gameMaster.GetComponent(MenuingDestroyed).escapeMenuText.GetComponent(FadingWithParent).enabled) {
		
			status = gameMaster.GetComponent(MenuingDestroyed).status;
			
			gameMaster.GetComponent(MenuingDestroyed).status = 3;

			gameMaster.GetComponent(MenuingDestroyed).escapeMenuScreen.SetActive(true);
			gameMaster.GetComponent(MenuingDestroyed).escapeMenuScreen.AddComponent(Appearing);
			
			gameMaster.GetComponent(MenuingDestroyed).escapeMenuText.SetActive(true);
			gameMaster.GetComponent(MenuingDestroyed).escapeMenuText.AddComponent(Appearing);
			
			gameMaster.GetComponent(MenuingDestroyed).escapeYesButton.SetActive(true);
			gameMaster.GetComponent(MenuingDestroyed).escapeYesButtonText.SetActive(true);
			gameMaster.GetComponent(MenuingDestroyed).escapeYesButtonText.AddComponent(Appearing);
			
			gameMaster.GetComponent(MenuingDestroyed).escapeNoButton.SetActive(true);
			gameMaster.GetComponent(MenuingDestroyed).escapeNoButtonText.SetActive(true);
			gameMaster.GetComponent(MenuingDestroyed).escapeNoButtonText.AddComponent(Appearing);
			
		}
		
	}
	
}


function OnClickEscapeNoButton () {

	gameMaster = GameObject.Find("GameMaster");

	if (gameMaster.GetComponent(MenuingDestroyed).status == 3){
	
		if (!gameMaster.GetComponent(MenuingDestroyed).escapeMenuText.GetComponent(Appearing)) {
		
			gameMaster.GetComponent(MenuingDestroyed).escapeMenuText.GetComponent(FadingWithParent).enabled = true;
				
			gameMaster.GetComponent(MenuingDestroyed).escapeYesButtonText.GetComponent(FadingWithParent).enabled = true;
				
			gameMaster.GetComponent(MenuingDestroyed).escapeNoButtonText.GetComponent(FadingWithParent).enabled = true;
			
			gameMaster.GetComponent(MenuingDestroyed).status = status;
				
		}
		
	}

}


function OnClickEscapeYesButton () {

	fader = GameObject.Find("GameMaster").GetComponent(MenuingDestroyed).fader;
	
	if (gameMaster.GetComponent(MenuingDestroyed).status == 3){
	
		if (!fader.GetComponent(Fading).enabled) {
		
		status = 0;
		
		fader.SetActive(true);
		fader.AddComponent(Appearing);
		
		Debug.Log(GameObject.Find("MainMenuObject").activeSelf);
		Debug.Log(GameObject.Find("MainMenuObject").activeInHierarchy);
		
		WaitAndLoadLevel(4, 0);
		
		}
		
	}
	
}


function WaitAndLoadLevel (time : float, level : int) {

	yield WaitForSeconds(time);

	Application.LoadLevel(level);

}

What in the world am I doing wrong? Can this error be caused by something else? I don’t even know what other information I could give… please ask …

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

I solved the same error on my project, so perhaps my experience can you help you find the answer.

In my case I was calling Dropdown.Hide() and then immediately deactivating the GameObject containing the Dropdown component. Turns out that Hide() contains this:

base.StartCoroutine (this.DelayedDestroyDropdownList (0.15f));

Judging by the name of the coroutine, DelayedDestroyDropdownList, I guessed that it hides the dropdown list after .15 seconds – the same dropdown which I had already hid. So I simply called my own coroutine which yielded for .15 seconds after calling Hide(), et voila! No more errors.

IEnumerator WaitThenHideDD ( Dropdown dd )
	{
		dd.Hide ();
		yield return new WaitForSeconds(.15f);
		// now it's safe to deactivate the dropdown game object
		RefreshStartUpUI();
	}

Maybe there’s something similar going on in your code.