- Home /
I won't be able to pursue troubleshooting this problem as we are no longer using this code.
Object reference to set to instance on an instantiated object
I have a C# script where I am instantiating a prefab inside of a coroutine. When I attempt to access that prefab from another coroutine or anywhere else in the code, I am getting a null reference exception; however, I know that the object exists, as I can see it on-screen, and I can reference it later in the same coroutine that instantiates it. Here is the relevant code:
private Vector3 originalPosition = new Vector3 (0, 0, 0);
public GameObject masterPrefab;
private GameObject instantiatedPrefab;
void Start ()
{
instantiatedPrefab = null;
}
private IEnumerator SpawnPrefab ()
{
if (instantiatedPrefab == null)
{
instantiatedPrefab = (GameObject)Instantiate (masterPrefab, originalPosition, new Quaternion (0, 0, 0, 0)) as GameObject;
}
print ("Prefab created at " + instantiatedPrefab.transform.position.ToString ());
}
If I call StartCoroutine (SpawnPrefab ()),
the prefab is instantiated. I can see it on the screen and it behaves as expected. The print statement accesses the instantiated prefab's position and prints it correctly. However, if I attempt to acces the instantiated prefab anywhere else -- in Update
, or another coroutine, for example -- I get the null reference exception. So the following will throw that error:
private IEnumerator DoSomethingWithPrefab ()
{
print ("Do something with prefab located at " + instantiatedPrefab.transform.position.ToString ());
}
As well as this:
void Update ()
{
print ("Prefab position: " + instantiatedPrefab.transform.position.ToString ());
}
I'm stumped. I figured if I successfully instantiated the object, I would be able to manipulate it within the script, but I'm clearly missing something.
Had this problem as well. What fixed my problem was by changing the type of the instantiated object to Transform ins$$anonymous$$d of GameObject:
private Transform instantiatedPrefab;
Answer by robertbu · Feb 15, 2013 at 07:42 PM
I tried to repo your problem and failed, so something more is going on. It occurred to me that you would have this problem if this script was attached to multiple objects, and you had masterPrefab initialized in only one of them. Here is the script I used to attempt to repro the problem:
public class Debug2 : MonoBehaviour {
private Vector3 originalPosition = new Vector3 (0, 0, 0);
public GameObject masterPrefab;
private GameObject instantiatedPrefab;
void Start ()
{
instantiatedPrefab = null;
StartCoroutine (SpawnPrefab ());
if (instantiatedPrefab == null)
Debug.Log ("null");
else
Debug.Log ("created");
}
private IEnumerator SpawnPrefab ()
{
if (instantiatedPrefab == null)
{
instantiatedPrefab = (GameObject)Instantiate (masterPrefab, originalPosition, new Quaternion (0, 0, 0, 0)) as GameObject;
yield return 0;
}
print ("Prefab created at " + instantiatedPrefab.transform.position.ToString ());
}
void Update ()
{
if (instantiatedPrefab != null)
print ("Prefab position: " + instantiatedPrefab.transform.position.ToString ());
}
}
Follow this Question
Related Questions
Referencing gameObject from script after Instantiate 0 Answers
NullReferenceException: Object reference not set to an instance of an object 1 Answer
Variable Assigning with GameObject.Find("") causing NullReferenceException? 0 Answers
Accessing a prefab after instantiating results as null 1 Answer
Having multiple objects fire prefabs in different times C# 0 Answers