Random

Hi i am wanting some way for unity to choose a random gameobject(out of 4 possible objects) using javascript. I want the game object chosen to be displayed on screen. can anyone steer me in the right direction?

You would create an array that has this for game objects in and then use Unitys Random class to get a random from 0 to 3 and return the game object at this index in the array

ok what i got from that was i need this

var hoop = GameObject[ ];

as the array. How do i specify which game objects need to be included? and which random class should i use?

This quick package does the trick, just take a look at how the folder is setup and how the javascript loads in the objects then instantiates them. Look through the manual for what each code block means.

See video for example

http://screencast.com/t/if5bMKdyW2XF

// class to hold our objects
class objects
{
	public var index;
	public var obj;
}

// used for the array
var theObjects;
// load in the objects from the resources folder
function Awake()
{
	theObjects=new Array();
	for(var i=1; i <5; i++)
	{
		var itm=new objects();
		itm.index = i;
		itm.obj =Resources.Load("obj"+i);
		theObjects.Add(itm);
	}
}

function OnGUI()
{
	 if (GUI.Button(Rect(10,70,100,30),"Random Object"))
	{
		// lets slap a random item from 1 through 4 into the game object called itm
		var itm=theObjects[Random.Range(0,4)];
		// now throw it on the screen at a random spot in viewing range of the camera :)
		 Instantiate (itm.obj, Vector3(Random.Range(-4,4), 1, Random.Range(4,-4)), Quaternion.identity);
    }
}

408099–14109–$RandomObjectsDemo.unitypackage (123 KB)

You should avoid using Resources.Load unless you actually need it for a reason. It’s simpler, more flexible, and avoids hard-coding stuff unnecessarily if you use the inspector, plus it allows Unity to manage your resources intelligently.

var hoop : GameObject[];

function Start () {
	Instantiate (hoop[Random.Range(0, hoop.Length)]);
}

–Eric

Thanks Eric thats helpful. the game chooses a random hoop(although its always hoop 4?) and puts it into the game. Yet i want it to just to display the number of the hoop on the screen(like the way the timer is displayed in a game) so the player can then find the right hoop with the same number, and once they have found this hoop(collision detection?) and then the game chooses another hoop.

hello.
me george.i am very happy to be a part of this forum.its too good from others.so keep it up and give me way to be strong.I want the game object chosen to be displayed on screen. can anyone steer me in the right direction?
thanks.

Welcome to the forum, george22!

If you use the code posted above, the GameObject will be visible as soon as it is instantiated. If you are doing this but can’t see the object on the screen then it is probably just appearing in the wrong position. There is actually another version of Instantiate that lets you choose the position and rotation of the object as it appears. If you don’t need to set the rotation, just use Quaternion.identity to use the neutral rotation:-

var startPosition: Vector3;  // Set this to whatever you need.
  ...

Instantiate (hoop[Random.Range(0, hoop.Length)], startPosition, Quaternion.identity);