- Home /
Question by
vitokvachadze · Nov 15, 2018 at 10:07 PM ·
instantiatetransform.positionfor-loop
How can I naming each instantiated object in "for" loop ?
There is my code to instantiated 3 object in a row
public GameObject clone;
void Start()
{
for (int i = 0; i < 3; i++)
{
Instantiate(clone, new Vector3(i , 0,0), Quaternion.identity);
}
}
And I want to write some code to change their positions individually.
void Update()
{
clone1.transform.position = new Vector3(...);
clone2.transform.position = new Vector3(...);
clone3.transform.position = new Vector3(...);
}
How can I naming each instantiated clone in "for" loop ?
Comment
Best Answer
Answer by Hellium · Nov 15, 2018 at 10:27 PM
public GameObject Prefab;
public int InstancesCount = 3;
private GameObject[] clones;
void Start()
{
if( InstancesCount > 0 )
{
clones = new GameObject[InstancesCount] ;
for (int i = 0; i < InstancesCount ; i++)
{
clones[i] = Instantiate(Prefab, new Vector3(i, 0,0), Quaternion.identity);
}
}
}
void Update()
{
for (int i = 0; i < clones.Length; i++)
{
clones[i].transform.position = new Vector3(...)
}
}
Your answer
Follow this Question
Related Questions
Cycle and Instantiate from an Array of GameObject 1 Answer
I need to move a clone of a prefab to a specific position after it instantiates 1 Answer
transform of instantiated bullet via ui button press not following the player 0 Answers
Prefabs wont instantiate using a for loop? 1 Answer
prefab always instantiates at 0.0.0 1 Answer