- Home /
Instantiate Random Object at Random Position
//Thoose are objects that i have;
var shooterPlatform : GameObject;
var wireFrame : GameObject;
var passObj : GameObject;
var bareels : GameObject;
//This is array List;
var randomObject;
//This is the variable of Randomly selected object; - not required
// var randomSelect;
function Awake()
{
//--------------------------------------------------------------------------
//At there, Instantiate the object that selected randomly
randomObject = new Array(shooterPlatform, wireFrame, passObj, bareels);
}
function Update () {
var randomSelect = Random.Range(0,(randomObject.length)); //since you want to get a random object every time
var position = transform.TransformPoint(Random.Range(-3.704676, 5.132769), 0, Random.Range(-3.704676, 5.132769));
//At there, Instantiate the object that selected randomly :S But it isn't worked
Instantiate(randomObject[randomSelect], position, Quaternion.identity);
}
I wrote this code for random object creation but it says "var randomSelect = Random.Range(0,(randomObject.length)); //since you want to get a random object every time"
I think there is en error Log says me : "lenght' is not a member of 'Object'. "
I'm really confused now.
There's no reason to have separate variables like that; just do
var objects : GameObject[];
and assign the prefabs to that array variable.
Answer by supercouge · Jul 19, 2013 at 09:18 PM
// in the declaration
var randomObject : Array;
Edit : For more information, take a look at the section #2 of Performance Optimization.
You should never use the Array class. It's slow and untyped. Use built-in arrays such as GameObject[]
, or generic Lists such as List.< GameObject >
.
Eric5h5 is right. You can found more information at this wiki page.
Allright Gentelman :) I wouldn't know this thanks all of you for comments and $$anonymous$$gestions. I'll keep them on my $$anonymous$$d.