- Home /
The question is answered, right answer was accepted
Instantiate from array into array?
Ok I have two arrays one with gameobjects and another with positions.
What I need is for the code to pick a gameobject at random and instantiate it at the positions once per position.
The bottom line is that I understand how to instantiate a gameobject into an array of positions but don't know how to instantiate an array of gameobjects into an array of positions. Randomly picking from gameobject array would be best too... and it has to be once per position not all into each position in the array of positions.
Here is the code(pseudo) so far: public GameObject[] models; public Transform[] instPos; //position transforms.
//This is the part I am lost at.
for(int i = 0; i < instPos.Length; i++){
// Instantiate(from models list? into instPos[i].... and so on...
//I'm confused help me please. Thanks.
}
My game work in progress: http://forum.unity3d.com/threads/200629-Omega-Void-WIP
Answer by ArkaneX · Jan 24, 2014 at 12:27 PM
Maybe not a super optimal, but quick solution. Requires System.Linq namespace.
var tmpList = models.ToList();
for(int i = 0; i < instPos.Length; i++)
{
var randomIndex = Random.Range(0, tmpList.Count);
var instance = (GameObject)GameObject.Instantiate(tmpList[randomIndex]);
instPos[i] = instance.transform;
tmpList.RemoveAt(randomIndex);
}
Untested, but even if there's any typo, general idea remains.
EDIT: I read the question again, and it seems I misunderstood it and you don't want to instantiate into the second array, but use positions from second array. In this case code would be:
var tmpList = models.ToList();
for(int i = 0; i < instPos.Length; i++)
{
var randomIndex = Random.Range(0, tmpList.Count);
var instance = (GameObject)GameObject.Instantiate(tmpList[randomIndex], instPos[i].position, instPos[i].rotation);
tmpList.RemoveAt(randomIndex);
}
Correct me if I'm wrong, but the random.Range's max value should be the arraylength - 1?
You're wrong :) I was just about to comment your answer. $$anonymous$$ax value parameter in Random.Range for floats is inclusive, but for integers it is exclusive.
Answer by NickP_2 · Jan 24, 2014 at 12:26 PM
public void SetPosition(GameObject[] models, Transform[] instPos)
{
ArrayList idsTaken = new ArrayList();
int id;
for (int i = 0; i < models.Length; i++)
{
id = Random.Range(0, models.Length - 1);
while (idsTaken.Contains(id))
{
id = Random.Range(0, models.Length - 1);
}
idsTaken.Add(id);
GameObject clone = Instantiate(models[id], instPos[i].position, transform.rotation) as GameObject;
}
}
I hope this is what you were searching for! Might be catchy with the while loop, but this should spawn 10 items, each at a position randomly chosen from an array filled with positions. Be sure to make the array of models
the same length as the intPos
length or an index error will occur!
GL!
Follow this Question
Related Questions
Instantiating from an object that's in an array 1 Answer
Instantiate Game Object on Array of Transforms 1 Answer
Cannot find the length of an array 3 Answers
Keep adding targets to a list 2 Answers
Rotating a Vector3 in Instantiation 1 Answer