- Home /
How do I find a local variables index in a foreach loop?
Hey all. I'm trying to set up a script that instantiates multiple objects of the same type in different stored positions with different stored rotations. I feel like I'm really close, but I'm having trouble with this one issue. I find all the objects of class Barrel in the scene, then store that in an array. I then go through a foreach loop, adding every position of that barrel to a list of barrelPositions. After that, I set up another foreach loop function, this time instantiating a barrelPrefab at every barrelPosition on the list, but since different barrels have unique positions, I'm trying to find the index of the local variable barrel and then use that index in the barrelPositions list for use in Instantiation.
Any help? Here's the relevant snippets of my code.
Barrel[] barrels;
Target[] targets;
Vector3 barrelStartPos;
Vector3 targetStartPos;
Quaternion barrelStartRotation;
Quaternion targetStartRotation;
List<Vector3> barrelPositions = new List<Vector3>();
List<Quaternion> barrelRotations = new List<Quaternion>();
List<Vector3> targetPositions = new List<Vector3>();
List<Quaternion> targetRotations = new List<Quaternion>();
void Start {
//find all barrels, then save their positions to the barrelPositions list
barrels = FindObjectsOfType<Barrel>();
foreach (Barrel barrel in barrels)
{
barrelStartPos = barrel.transform.position;
barrelPositions.Add(barrelStartPos);
}
//find all barrels, then save their rotations in the same order as barrelPositions
foreach (Barrel barrel in barrels)
{
barrelStartRotation = barrel.transform.rotation;
barrelRotations.Add(barrelStartRotation);
}
}
public void RespawnObjects()
{
//instantiate every barrel with the same rotation and positions as those with the same index in their respective lists
foreach (Barrel barrel in barrels)
{
//int barrelIndex = barrels.index //here's the annoying part
Instantiate(barrelPrefab, barrelPositions.barrelIndex, barrelRotations.barrelIndex);
}`
use a foor loop
for(int i = 0; i < barrels.length; i++) { Instantiate(barrelPrefab, barrelPositions[i], barrelRotations[i]); }
tell me if this worked :)
Doesn't seem to, no. Nothing is respawning.
debug.log barrels.length and check how many objects should spawn
int barrelIndex = barrels.IndexOf(barrel)
Though is there a reason you do not want to use a for loop?
Answer by Taylor-Libonati · Mar 15, 2019 at 09:54 PM
If you need to keep track of an index the easiest way is to just make an int and add 1 each time you loop in the function. Like this:
//Create a Variable
int i=0;
//Loop through barrels
foreach (Barrel barrel in barrels)
{
Instantiate(barrelPrefab, barrelPositions[i], barrelRotations[i]);
//Add 1 to index
i++
}
Also it looks like you are never calling the spawn barrels function so that code will never actually run. Side note: You don't need two for each loops in your Start function. just move the rotation code right below the position code.
I do call the function in another script, so the functions do run. The barrels will respawn, but they will often have duplicates with the same transform if they were not destroyed previously. How would you go about removing the old objects, or only spawning them if they were destroyed?
If you want to delete all the barrels after you save their positions you just use the Destroy function in Start. You can put it in the loop you already have or make a new one if you need too.
Destroy(barrels[i].gameObject);
Perhaps ins$$anonymous$$d of destroying them you can deactivate them ins$$anonymous$$d; then ins$$anonymous$$d of Instantiating when respawning, you check if it is not active; then reactivate and reset the position and rotation.
Honestly, I just went with this in the end. I was told it wasn't "Good practice" to do this sort of thing, but it seems like it's just so much lighter in both performance and stress. Thanks.
Thanks you ! It's very useful and simple. But if we want to begin to the index 0 from the loop, the variable "i" need to be "-1" and not "0" !
Sorry but that doesn't make much sense. Have you actually had a closer look at the code that Taylor posted? He uses "i" inside the loop and at the very end when the loop is done he increments i. Since i starts at "0" it means that i will be 0 during the first iteration. You do not increment "i" at the beginning of the loop.