- Home /
How can I use foreach and a list of vector3s to reset the positions of all child objects?
I have a script that makes a list of the positions of all the child objects when the program starts. I want to use this list to reset all child objects to their position when the resetPos() function is called but I'm not sure how to do this? What should I put in my foreach function under the resetPos() function?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class resetPositions : MonoBehaviour
{
List<Vector3> originalPos = new List<Vector3>();
// Start is called before the first frame update
void Start()
{
foreach (Transform child in transform)
{
originalPos.Add(child.position);
}
}
public void resetPos()
{
foreach (Transform child in transform)
{
}
}
// Update is called once per frame
void Update()
{
}
}
Answer by faizit9 · Apr 01, 2021 at 01:44 AM
Nvm, I found a solution. for anyone with the same problem, do this:
int i = 0;
foreach (Transform child in transform)
{
child.position = originalPos[i];
i++;
}
Your answer
Follow this Question
Related Questions
null reference exception when I already checked if this was null 1 Answer
How would I find the name of all game objects in a c# List 1 Answer
"enumeration operation may not execute" Problem with C# list 1 Answer
C# Adding Items to list in Foreach not working 1 Answer
How do you interate through a JSON object with a foreach loop to add each element to a list? 0 Answers