Random spawn script

Hello everyone,

Im extremely new to Unity, although i have some previous low-level development experience. I’ve been learning Unity by creating my own simple experimental projects, as i find that the easiest way to learn something new.

Right now i’ve stumbled upon a problem that im having with a demo game of mine, the basic idea of it is a simple catch game, where objects fall from the top of the screen, and i have to catch them with a basket. The base for my experiment is a sample created by Mike Hergaarden, and it can be found from here.

Im using a simple spawn script provided with the example, but i would like to randomize the spawning system so that there are different kind of objects spawning. For example there could be three kind of eggs with different colours and each having their own prefabs.

How would such randomization be implemented in scripting? Any help is appreciated, even though there wouldn’t be an exact example available. This is most likely not a big issue, but at the moment im a bit overwhelmed by all the new information.

Thanks!

Use Random.Range. For example, you may have a red egg, a blue egg and a green egg.

var eggs : Transform[]; //Creates an array of your various egg prefabs (red egg, blue egg, green egg)
var eggType : float; //Randomises egg type

function Update () {
	eggType = Random.Range(0, eggs.length);
	Instantiate(eggs[eggType], Vector3.zero, Quaternion.identity); //Instantiates random egg at 0,0,0 (you can change) and with no specific rotation (you can also change)
}

Good luck! Klep