Hi, I want to delete the specific element in array. I’m trying to spawn the objects at random locations. I’m storing some random positions in array to randomly spawn the objects. So I want to delete the spawned position so that object won’t spawn again at that position. How do I do this? and I’m using javascript.
You should use a list instead. Array could do but you would have to keep track of indexes or in the end you would be doing manually what a list does automatically.
List<Vector3> positions = new List<Vector3>();
positions.Add(new Vector3(0,0,0));
Do this for all positions and then you can use like below:
int ind = new Random(0, positions.Count);
Instantiate(prefab, positions[ind],Quaternion.identity);
positions.RemoveAt(ind);
Your list will remove the value at ind and shrink automatically. The useless data will collected on the next garbage collection. positions.Count will also decrease to amke sure you do not get an out of bound index.