- Home /
Instantiated object from asset bundle is null
Hello, I am instantiating an object from asset bundle. That works fine, but when I try to check if the object's prefab is not null to call another function, it returns null.. Here's the code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace RoboGarden
{
public class CollectiveObject : FieldObject
{
public CollectiveObject(int x, int y, string name,string type, float width = MainController.CELL_SIZE, float height = MainController.CELL_SIZE) : base(x, y, name, "collective", type, width, height)
{ }
public GameObject logo = GameObject.FindGameObjectWithTag("logo");
AssetBundleRequest request;
MonoBehaviour temp = GameObject.FindObjectOfType<MonoBehaviour>();
public IEnumerator ObjectCreator()
{
LoadingMenu myAssets = logo.GetComponent<LoadingMenu> ();
request = myAssets.wwwww.assetBundle.LoadAssetAsync (type, typeof(GameObject));
yield return request;
GameObject objj = request.asset as GameObject;
objj.transform.localScale = new Vector3 (MainController.SCALE * MainController.CELL_SIZE / realW, MainController.SCALE * MainController.CELL_SIZE / ((realW + realH) / 2), MainController.SCALE * MainController.CELL_SIZE / realH);
float X = (xIndex * MainController.SCALE * objectWidth) - (MainController.SCALE * objectWidth / 2);
float Y = (yIndex * MainController.SCALE * objectHeight) - (MainController.SCALE * objectHeight / 2);
obj = UnityEngine.Object.Instantiate(objj, new Vector3(X, 0, Y), Quaternion.identity) as GameObject;
obj.name = objectName;
}
public bool inti()
{
temp.StartCoroutine(ObjectCreator ());
if (obj != null) {
Debug.Log ("Found it !");
return true;
}
else {
HelperFunctions.consolePrint(type + " is to found in reasouces");
return false;
}
}
}
}
Edit: I figured that Coroutines are causing it, the check happens before coroutine finishes. Why? What am I doing wrong?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace RoboGarden
{
public class CollectiveObject : FieldObject
{
public CollectiveObject(int x, int y, string name,string type, float width = MainController.CELL_SIZE, float height = MainController.CELL_SIZE) : base(x, y, name, "collective", type, width, height)
{ }
public GameObject logo = GameObject.FindGameObjectWithTag("logo");
AssetBundleRequest request;
MonoBehaviour temp = GameObject.FindObjectOfType<MonoBehaviour>();
public IEnumerator ObjectCreator()
{
LoadingMenu myAssets = logo.GetComponent<LoadingMenu> ();
request = myAssets.wwwww.assetBundle.LoadAssetAsync (type + ".prefab", typeof(GameObject));
while(!request.isDone) {
yield return new WaitForSeconds (2f);
}
GameObject objj = request.asset as GameObject;
obj = GameObject.Instantiate (objj) as GameObject;
obj.transform.localScale = new Vector3 (MainController.SCALE * MainController.CELL_SIZE / realW, MainController.SCALE * MainController.CELL_SIZE / ((realW + realH) / 2), MainController.SCALE * MainController.CELL_SIZE / realH);
float X = (xIndex * MainController.SCALE * objectWidth) - (MainController.SCALE * objectWidth / 2);
float Y = (yIndex * MainController.SCALE * objectHeight) - (MainController.SCALE * objectHeight / 2);
obj.transform.position = new Vector3 (X, 0, Y);
obj.name = objectName;
yield return obj;
}
public IEnumerator DummyFunction()
{
yield return temp.StartCoroutine (ObjectCreator ());
}
public bool inti()
{
temp.StartCoroutine (DummyFunction ());
if (obj != null) {
Debug.Log ("Found it!");
return true;
} else {
HelperFunctions.consolePrint ("Not found");
return false;
}
}
}
}
If you need the coroutine to have finished before you move on to the next thing, then you need to wait for it to finish. It looks to me like you're just starting it (on line 45) and then immediately checking the value of obj
, which the coroutine won't have set yet (because there are yields beforehand). There are several possible ways of doing this. You could for example have the coroutine itself end by kicking off whatever has to happen next. Or another way would be to call the coroutine from another coroutine using yield return StartCoroutine()
, so that the outer one waits for the inner one to finish before moving on.
Thing is I cannot set this bool inti() to IEnumerator, because another script depends on its returned bool value. I tried IEnumerator , but when I try to write an if condition that checks the returned value, it tells me that I cannot convert Coroutine to bool..
That's right, you can't return a value from a coroutine like that. There are other ways to achieve what you're after though.
For example, your coroutine could set a boolean field in the class which the subsequent code can then check ins$$anonymous$$d of using a return value. This can work in simple cases but it has the drawback that all calls to the coroutine share the same flag.
A more robust and flexible approach is to pass a callback function (with a single bool parameter) to the coroutine, and then when the coroutine has finished, call that function passing in the bool that you want to "return" from the function. Your callback function contains the code you want to execute next.