The question is answered, right answer was accepted
Instantiate prefab by total int
hi, i wonder how to instantiate some prefab by int ? i'll make it clear. i'm making a strategy games , and there are soldiers which it generated by the turn pass. so i put the soldier number by int , but i don't know how to instantiate the total number become total prefab.
Answer by Cynikal · Aug 30, 2016 at 06:39 AM
If I understand correctly, you want to input a number, and instantiate X number of units? If you want to instantiate say... 50 units.
void InstantiateMany(int Number, GameObject What2Instantiate, Vector3 Position)
{
for (int i = 0; i <= Number ; i++)
{
Instantiate(What2Instantiate, Position, Quaternion.identity);
}
}
Then use like:
InstantiateMany(50, MyPrefab, new Vector3(1f,0f,0f));
That'll spawn 50 MyPrefab's at the location of 1f, 0f, 0f. The issue with this is, they will literally spawn on top of one another. You'll have to do your own logic to spread them out.
so i'll need get vector3 and random it location prefab? thanks! it worked.