- Home /
Coroutine couldn't be started because the the game object 'LevelManager' is inactive!
I'm getting this error when I'm trying to run a function from another script on the GameObject "LevelManager" (all javascript):
saveScript.Load(saveScript.levelName);
From what I gather, the error usually occurs when you try and call a function on a GameObject that is disabled. However, I have no code for disabling, and when I run the following, it tells me the GameObject is active both before and after my function call.
Debug.Log("Active?: " + GameObject.Find("LevelManager").active);
saveScript.Load(saveScript.levelName);
Debug.Log("Active?L " + GameObject.Find("LevelManager").active);
This function used to work fine a few days ago, I just noticed it stopped working, I don't know when or why. The weird part is I can call a test function on saveScript on the line before and it'll run fine.
Help is appreciated! =)
EDIT: Here's the saveScript (it's in Standard Assets):
#pragma strict
import System.Collections.Generic;
import System.IO;
import Parse;
import System.Threading.Tasks;
public static var filePath : String;
public static var levelName = "$TEMP$";
public static var receivedMessage;
static var fakeball : GameObject;
static var saveTask : Task;
function Start () {
if (Application.isEditor)
filePath = Application.dataPath;
else
filePath = Application.persistentDataPath;
}
public function Save(upload : boolean) {
Debug.Log("Saving");
if (GameObject.Find("LevelName"))
levelName = GameObject.Find("LevelName").GetComponent(UI.InputField).text;
if (levelName == "" || levelName == null)
GameObject.Find("ResultText").GetComponent(UI.Text).text = "Please Enter a Level Name";
else
{
var gos = GameObject.FindGameObjectsWithTag("Pads") + GameObject.FindGameObjectsWithTag("Diamonds") + GameObject.FindGameObjectsWithTag("Wood") + GameObject.FindGameObjectsWithTag("Keys");
var sPath : String = filePath + "/Levels/MyLevels/" + levelName + ".txt";
var sData : StreamWriter = new StreamWriter(sPath);
sData.WriteLine(levelName);
for (var i = 0; i < gos.Length ; i++)
{
sData.WriteLine(gos[i].name + "|" + gos[i].transform.position.ToString() + "|" + gos[i].transform.rotation.ToString() + "|");
}
sData.Flush();
sData.Close();
if (upload) {
var parseSave = Instantiate(Resources.Load("ParseSave"), Vector3(0,0,0), Quaternion.identity);
parseSave.name = "ParseSave";
}
}
Debug.Log("Saved!");
}
public function ParseLoad(level : String) {
levelName = level;
var parseLoad = Instantiate(Resources.Load("ParseLoad"), Vector3.zero, Quaternion.identity);
parseLoad.name = "ParseLoad";
}
public function ReloadLevel() {
Debug.Log("Reloading...");
if (!(Application.loadedLevelName == "MainMenu"))
{
yield StartCoroutine(GameObject.Find("LevelManager").GetComponent(saveScript).Load (levelName));
DestroyImmediate(GameObject.Find("Ball"));
var ball = Instantiate(Resources.Load("Ball"), GameObject.Find("FakeBall").transform.position, Quaternion.identity);
ball.name = "Ball";
Destroy (GameObject.Find ("FakeBall"));
ballScript.shotsTaken = 0;
GameObject.Find("ParText").GetComponent(UI.Text).text = ballScript.shotsTaken.ToString();
}
else
GameObject.Find("Ball").transform.position = Vector3(0,30,0);
}
public static function Load(level : String) {
Debug.Log("LOADING");
var gos = GameObject.FindGameObjectsWithTag("Pads") + GameObject.FindGameObjectsWithTag("Diamonds") + GameObject.FindGameObjectsWithTag("Wood") + GameObject.FindGameObjectsWithTag("Keys");
for (var j = 0; j < gos.Length ; j++)
Destroy(gos[j]);
yield WaitForEndOfFrame();
if (System.IO.File.Exists(filePath + "/Levels/Cache/" + level + ".txt"))
{
levelName = level;
var lPath : String = filePath + "/Levels/Cache/" + level + ".txt";
var lData : StreamReader = new StreamReader(lPath);
var title = lData.ReadLine();
for (var i = 0; !lData.EndOfStream ; i++)
{
var line = lData.ReadLine();
var parsedLine = line.Split("|"[0]);
var c = parsedLine[1].Substring(1,parsedLine[1].Length-2).Split(","[0]);
var quat = parsedLine[2].Substring(1,parsedLine[2].Length-2).Split(","[0]);
var item = Instantiate(Resources.Load(parsedLine[0]), Vector3(float.Parse(c[0].Trim()),float.Parse(c[1].Trim()),float.Parse(c[2].Trim())), Quaternion(float.Parse(quat[0].Trim()),float.Parse(quat[1].Trim()),float.Parse(quat[2].Trim()),float.Parse(quat[3].Trim())));
item.name = parsedLine[0];
}
lData.Close();
}
globalScript.ResetVars();
if (!GameObject.Find("FakeBall"))
{
fakeball = Instantiate(Resources.Load("FakeBall"), Vector3.zero, Quaternion.identity);
fakeball.name = "FakeBall";
}
if (GameObject.Find("UserLevelsCanvas"))
Destroy(GameObject.Find("UserLevelsCanvas"));
Debug.Log("LOADED!");
}
You should paste the SaveScript class.
You could also put a debug in the Start of it to see if you would not have it on another object which happens to be inactive.
void Start(){Debug.Log(name);}
Added, the saveScript is on a gameobject that is never destroyed (and kept between Application.Load's) so it should never be inactive
EDIT: Playing around with it, I found that it is Line 73 causing the problem, however I need it there in order for it to work properly. Is there an alternative way of writing it?
Answer by lordlycastle · Apr 20, 2015 at 03:18 PM
The Load functions is static function, and aren’t you calling it with a instance? Either remove the static modifier or use the class name instead of instance. Also the return type of any function that contains yield should be IEnumerator.
I'll try and fix the static issue. I'm program$$anonymous$$g in javascript, I thought IEnumerator only exists (and is needed) in C#?
EDIT: I removed "Static" from the function (had to fix a few references to it too), and then made a function in the saveScript called Wait() that just had yield WaitForEndOfFrame(); and called that function ins$$anonymous$$d of directly using the yield inside the Load() function. It's a pretty roundabout way, the complexity of yield confuses me, but at least it's working perfectly! Cheers man!
Your answer