- Home /
How can I Instantiate an Object from a Randomly Selected Index in an Array of Game Objects?
Basically, I have a bunch of Game Objects tagged as "Evidence Locations" and "Evidence".
When the game starts, a script finds all the objects tagged with both of those:
var tagToSearchFor : String;
var evidenceLocationTag : String;
function Start () {
var evidenceList : GameObject[];
evidenceList = GameObject.FindGameObjectsWithTag(tagToSearchFor); //get all the GO's tagged with the appropriate tag
var evidenceLocation : GameObject[];
evidenceLocation = GameObject.FindGameObjectsWithTag(evidenceLocationTag); //get all the GO's tagged with the appropriate tag
}
then while there is less than a set amount of evidence present, we run a random number to find the type of evidence we use, then again to find the random location it will occur at, then instantiate the evidence from that array that was chosen, and the location, and lastly, increment the amount of evidence present:
while (evidenceLocLoop <= 10)
{
randomEvidence = Random.Range(0, evidenceList.Length);
randomLocation = Random.Range(0, evidenceLocation.Length);
tempClone = Instantiate(evidenceList[randomEvidence], evidenceLocation[randomLocation], transform.rotation);
evidenceLocLoop++;
}
I know this doesn't account for the possibility of reusing a location yet, and that will be handled later, but my question is, what is the syntax, or how can I handle Instantiating the randomly chosen piece of evidence and randomly chosen location correctly?
Currently I get the error, "No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list'(UnityEngine.GameObject, UnityEngine.Quaternion)' was found."
Thanks in advance!
Answer by Eric5h5 · Nov 06, 2012 at 03:17 AM
Instantiate takes an object, a Vector3, and a quaternion. You can't pass a GameObject, a GameObject, and a quaternion. Perhaps you meant evidenceLocation[randomLocation].transform.position.
Your answer
Follow this Question
Related Questions
Instantiate an array of prefabs? 0 Answers
instantiate a random object from multiples via 'tag' 1 Answer
How to instantiate the first 8 gameobjects in an array (UnityScript)? 1 Answer
multiple object in multiple spawn Point with out repeat 0 Answers
Randomly instantiate objects from array without choosing the same item twice. 2 Answers