Random Number Generation Problem

hello,

I have 6 different location and when the user press “P” my key should display in different location each time.

var Location : Transform [];
var Key : Transform;

function Update () {

if(Input.GetKeyUp (KeyCode.P))
    {
     var i = Random.Range(0, 6);
       
		Instantiate(Key, Location*.position, Quaternion.identity);*
*	}*
*}*
*
*

where am i wrong in this code…

Key should be a GameObject and not a Transform, and of course it should be a valid Prefab in your Project.

everything is working fine other than finding random value… if i change the Location[2], my key object is coming in Location[2] perfectly… But it showing Random.Range error… im not getting the random value i…

Aah, i think i get it now, Right now you’re chosing between 7 numbers, not the 6 numbers you want, when you’re using the random.range “0” counts as a number as well, try using random.range(0,5) instead

Edit; as the above answer seems to be wrong, i’ll try again :stuck_out_tongue:

could you try rewriting the code so it looks like this?:

var Location : Transform [];
var Key : Transform;

function Update () {

if(Input.GetKeyUp (KeyCode.P))
    {
		Instantiate(Key, Location[Random.Range(0,6)].position, Quaternion.identity);
	}

}

MissingMethodException : Method not found Random.Range

This is the error im getting in console…

This code seems ok: if you fill the Location array with 6 different empty objects and Key with the prefab, each time you release key P a new Key object should be created in one of the Location positions. Besides initialization errors (Location or Key not assigned), the only thing I think could go wrong with this code is the the Location objects having too low Y coordinates, what would make the objects be created under the terrain.

no i kept the location very clearly and above the terrain only…

Kacer… i tried your code but still getting the Method not Found error in the console… i became so sick for finding the solution for this simple task… I think its the Unity problem, i think i might not installed the unity properly…

Guys finally i fixed this problem… thanks for ur support… thanks to everyone who helped me…