- Home /
UnityException: Transform child out of bounds.
I'm getting the error in the title from this line:
droneArray[c] = Instantiate(droneConstruct,spawnpointStack.GetChild(spc).position,Quaternion.LookRotation(transform.position));
EDIT So I fixed the problem, but now I'm getting this:
"NullReferenceException: Object reference not set to an instance of an object"
Why, Unity! Why!
Answer by CHPedersen · Apr 12, 2012 at 05:59 PM
As a general rule of thumb, a NullReferenceException always means you are attempting to access a member of an object, be it class field or method, the reference to which is null. This typically occurs because an attempt to acquire a reference failed, such as through the use of GetComponent where a scriptname is spelled wrong, the gameobject doesn't have that component attached at all, or perhaps a call to GameObject.Find where the wanted gameobject's name is invalid.
It's hard to know just what's wrong in your case because you only posted one line of code, naturally. But if I were to venture a quick guess, anyway, I'd bet it's probably spawnpointStack being null. Check the state of that object before the line you posted by simply shooting the variable into a call to Debug.Log. Does that print "null" to the console?
If not, it is likely the prefab you're trying to instantiate a copy of, that is, the object called "droneConstruct". Is this a public variable in your script which is supposed to reference a prefab in your project? If so, have you remembered to drag the prefab onto that variable in the editor?
Your answer
Follow this Question
Related Questions
Instantiating a new gameObject as a child of a different gameObject 2 Answers
2D platformer firing left and right, help! 1 Answer
Delete children of Instantiated object 2 Answers
Instantiate a Prefab as child 0 Answers
Creating new Transform from existing objects Transform to Instantiate object 1 Answer