The question is answered, right answer was accepted
[SOLVED] Remove Vector3 Array step by step when GameObject spawned
I store positions of gameobjects in Vector3 array like this
public Vector3[] objectPositions;
To spawn gameobject I use this variable
public GameObject object;
I can spawn my object which stored in objectPositions array.
And from here the game is going to loop and spawning a lot of objects till to unity crash. The question is:
I want to spawn object given at certain coordinates at vector3 array and when I spawn object and remove coordinates in vector3 array step by step when spawning objects. I spawn object using "for" loop
for (int x = 0; x < objectPositions.Length; x++)
{
Instantiate(object, objectPositions[x], Quaternion.identity);
// I want to remove vector3 array step by step when spawning object
// When object spawns and remove that coordinate at array then spawn the next object and remove
// the next coordinate till is getting empty the vector3 array
}
Because if I don't remove that coordinates at vector3 array and objects spawn till crash. And because of this to stop this loop I need to remove each coordinates at vector3 array when each object spawn. How can I do this
Answer by KoenigX3 · Apr 04, 2021 at 09:25 AM
I don't understand why you should remove the coordinates. If you loop through an array of 10-20-30 entries, it shouldn't crash Unity, unless you do it on Update.
Anyway, if you really need to remove the entries, consider using a List. Also, you should not use a for loop when removing array elements.
public List<Vector3> objectPositions;
while(objectPositions.Count > 0)
{
Instantiate(object, objectPositions[0], Quaternion.identity);
objectPositions.RemoveAt(0);
}