- Home /
Question by
dlecky · Apr 10, 2014 at 09:05 PM ·
nullreferenceexception
NullReferenceException: Object reference not set to an instance of an object
For the life of me I cannot understand why I get the error: NullReferenceException: Object reference not set to an instance of an object CreateRandomPoulpi.Start () (at Assets/Scripts/CreateRandomPoulpi.cs:21)
The line its complaining about is: octo.transform.parent = newParent.transform;
public class CreateRandomPoulpi : MonoBehaviour {
public GameObject enemyPrefab;
public float numEnemies;
public float xMin = 19F;
public float xMax = 85F;
public float yMin = 3.5F;
public float yMax = -4.5F;
void Start () {
GameObject newParent = GameObject.Find("1 – Background Elements");
for (int i = 0; i < numEnemies; i++)
{
Vector3 newPos = new Vector3(Random.Range(xMin, xMax), Random.Range(yMin, yMax), 0);
GameObject octo = Instantiate(enemyPrefab, newPos, Quaternion.identity) as GameObject;
octo.transform.parent = newParent.transform;
}
}
}
Any help is much appreciated.
Comment
Answer by Spoink · Apr 10, 2014 at 09:13 PM
By the looks of it, unity cant find "1 – Background Elements" in the scene. Do note that if the object is deactivated GameObject.Find will not find the object in the scene.
Your answer
