Help with my code ArgumentException: The prefab you want to instantiate is null. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message)

So I am making a game that will spawn a random game object out of a 10 game objects in an array. The game object is an image put in a empty game object. I don’t know whats wrong with my code but can someone help please? I keep getting this error: ArgumentException: The prefab you want to instantiate is null.
UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message)

#pragma strict
var obj : GameObject[];
	obj = new GameObject[10];
function Start () {

}

function Update () {
var randomObj = Random.Range(0,obj.length);
	var randomNum : int;
	randomNum = randomObj;
	var theObj = obj[randomNum];
	Instantiate(theObj, Vector3.zero, Quaternion.identity);
}

1 Answer

1

Initialization your array in Inspector (as public) or in Start():

 var obj : GameObject[];
 
 function Start () {
  obj = new GameObject[10];
  //Set up for each element game object
  obj[0] = GameObject.Find("myGameObject");
  //And etc
 }

I hope that it will help you.