Randomly instantiate prefab gameobjects

The below code will spawn my cubePrefab when I click on the tube collider. I have a total of 4 prefabs that I would like to spawn randomly when the collider is clicked on. Any ideas for this?

var cubePrefab : GameObject;

function Start () {
cubePrefab = Resources.Load(“cubePrefab”);
}

function Update () {
var ray : Ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit = new RaycastHit();
if(Input.GetMouseButtonDown(0))
{
if(Physics.Raycast(ray,hit))
{
//Weve hit something
if(hit.collider.gameObject.name.Equals(“tube”))
{
//weve hit the button
CreatShape(hit.point);
}
}
}
}

function CreatShape(pos : Vector3)
{
Instantiate(cubePrefab, pos, Quaternion.identity);
}

Please learn to use the code tags:

[code][/code]

Just do this:

var cubePrefabs : GameObject[];

...

Instantiate(cubePrefabs[Random.Range(0, cubePrefabs.Length)], pos, Quaternion.identity);

You should link the prefab(s) in the inspector unless you have very good reasons to use Resources.Load().

If you want to instantiate one of the four prefabs then store them in your cubePrefab (or cubePrefabs) property an array (instead of just one) and then choose one of those at random from within your CreateShape function.

Edit: Matt beat me to it. :slight_smile:

I don’t think I understand what you are doing with that code exactly. Each of my prefabs are a diff name and I was thinking I would declare each one first and then Instantiate them randomly.

var cubePrefab : GameObject;
var rectPrefab : GameObject;
var trectPrefab : GameObject;
var lrectPrefab : GameObject;

I hope this helps clarify what I am trying to. I just didn’t understand how your code was working.

Thanks

Thank you guys so much!!! After I played around with the code for a bit I got it figured out. I have no programming experience what so ever.

Why would you need a very good reason for that? What’s the drawback? For example in my case I want to dynamicly populate a map with objects, which I read from a text file, like so:

Transform AddMapItem(string type, float x, float y)
{
   Transform t = Resources.Load(type);
   t.position = new Vector3(x, 0, z);
   return t;
}

What’s the risk here?

If you’re going to do things in a data-driven way (reading a text/XML file, pulling from a database, etc.) then you’re doing things properly - that’s a ‘very good reason’ in my book. :slight_smile: So, there’s no “risk” at all, you simply never provided that as background before.

Carry on!

Maybe off topic but about the Resources folder:

Everything you put there will be in the deployed game. So if you start using Resources.Load() vigourously it may lead to a game that is heavier than intended. I can imagine especially for web-based deployment this could be very frustrating.

So it’s wise to ponder if you really need to put it in the resources folder.

As to the random-function. If you want it to be scalable maybe you should create an empty GameObject and attach a script to it that handles the function of randomly selecting an object? In that way you’d be able to just call this script from whatever in your scene that wanted a random object to appear. Just a thought.