- Home /
How to instantiate a random object from an array?
I am trying to make a Tetris clone for practice but i cannot get my objects to spawn randomly. I'm still a noob to this any help would be greatly appreciate.
public int pieceSpawn;
public GameObject linePiece;
public Transform spawnLocation;
public GameObject[] pieces;
void Update ()
{
pieces = new GameObject[7];
pieces[0]=GameObject.FindGameObjectWithTag("cube");
pieces[1]=GameObject.FindGameObjectWithTag("line");
pieces[2]=GameObject.FindGameObjectWithTag("L");
pieces[3]=GameObject.FindGameObjectWithTag("J");
pieces[4]=GameObject.FindGameObjectWithTag("s");
pieces[5]=GameObject.FindGameObjectWithTag("Z");
pieces[6]=GameObject.FindGameObjectWithTag("T");
pieceSpawn = UnityEngine.Random.Range (0,7);
if (Input.GetKeyDown ("down")) {
Instantiate (pieces[UnityEngine.Random.Range(0,6)], spawnLocation.position, spawnLocation.rotation);
Looks pretty good. You don't say what is currently going wrong so it's hard to help. What is happening and how do you want to change it. What do you mean by "spawn randomly" ? The ti$$anonymous$$g, position, rotation ?
Everything is fine until I hit play then i get this: ArgumentException: The prefab you want to instantiate is null
$$anonymous$$ake sure you have actually put something inside your gameobject array.
Which part fails? You should be able to use Random.Range without the prefix as UnityEngine should probably be linked as a namespace.
The code indeed looks good, so it means your GameObject.FindGameObjectWithTag couldn't find a gameobject and returned null. Have you tried iterating over the array and logging the gameobjects, it should show some "null" logs.
Also you shouldn't use the FindObject method in the Update, ins$$anonymous$$d search your objects in Awake or Start..
Answer by hexagonius · Mar 05, 2015 at 07:44 PM
I would recommend that you create a prefab for each GameObject (Drag each Gameobject from the Hierarchy somewhere into the Project panel). Then assign all of them to the pieces array in the inspector (Drag them onto the name and they'll add up). This will also reduce the possibility of making a mistake. THEN, you can shorten your code and make it bulletproof:
void Update ()
{
if (Input.GetKeyDown ("down")) {
Instantiate (pieces[Random.Range(0,pieces.Length)], spawnLocation.position, spawnLocation.rotation);
}
}
Answer by iahr · Oct 03, 2016 at 02:10 PM
Get rid of "FindObjectWithTag". It will be always return null because the GameObject you want to "FIND" is not in the "game" already.
Jus use this code
public Transform spawnLocation;
public GameObject[] pieces;
void Update (){
if (Input.GetKeyDown ("down")) {
Instantiate (pieces[UnityEngine.Random.Range(0,6)], spawnLocation.position, spawnLocation.rotation);
}
}
In the inspector, put how many GameObject you prefer to choose to instantiate. then Drag the gameobjects in the inspector.
Answer by GunLengend · Oct 03, 2016 at 02:10 PM
Make all of initial in Start()
void Start()
{
pieces = new GameObject[7];
pieces[0]=GameObject.FindGameObjectWithTag("cube");
pieces[1]=GameObject.FindGameObjectWithTag("line");
pieces[2]=GameObject.FindGameObjectWithTag("L");
pieces[3]=GameObject.FindGameObjectWithTag("J");
pieces[4]=GameObject.FindGameObjectWithTag("s");
pieces[5]=GameObject.FindGameObjectWithTag("Z");
pieces[6]=GameObject.FindGameObjectWithTag("T");
}