- Home /
Instantiate random object from Array
Hello Guys, I'm 15 years old applicaton developer from turkey. So I have a problem about a script.
#pragma strict
//---------------------------------------
var waitFor : int;
var waitMin : int;
var waitMax : int;
//-----------------------
var possibleLeft = new Array(RCloud1, RCloud2, RCloud3, RCloud4);
var possible;
//-----------------------
var RCloud1 : GameObject;
var RCloud2 : GameObject;
var RCloud3 : GameObject;
var RCloud4 : GameObject;
//-----------------------
function Awake (){
possibleLeft = Random.Range(0, possibleLeft.length -1);
}
function Start () {
cloudScript ();
}
function cloudScript () {
waitFor = Random.Range(waitMin, waitMax);
Instantiate (possibleLeft, gameObject.transform.position, Quaternion.Euler(0, 180, 0));
yield WaitForSeconds(waitFor);
}
There's my code. When i save it Debug Log gave me theese errors.
1 = "Cannot convert int to Array" (28,28)
2 = "No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(Array, UnityEngine.Vector3, UnityEngine.Quaternion)' was found." (44,13)
What shall i do ? Please help me ! I'm stuck on this part. I want to create random clouds that will move around from one spawner. Thanks for reply.
Answer by tgf · Nov 03, 2013 at 03:47 PM
You are trying to assign int random value to array object. Maybe you meant: possible = possibleLeft[Random.Range(0, possibleLeft.length -1)];
Answer by Zaeran · Nov 03, 2013 at 02:10 PM
In your awake function, you are trying to assign an integer to an array value.
You're trying to instantiate an array instead of a gamoObject.
In both of these instances, change 'possibleLeft' to 'possible'
Relevant code:
//1.
function Awake (){
possible = possibleLeft[Random.Range(0, possibleLeft.length-1)];
}
//2.
Instantiate (possible, gameObject.transform.position, Quaternion.Euler(0, 180, 0));
Your answer
Follow this Question
Related Questions
How Do I Add An Instantiated Object To An Array? 3 Answers
Object Spawning Randomly 0 Answers
Physic2D.overlapCircle Instantiation 0 Answers
can you instantiate random prefabs from the resource folder? 2 Answers