- Home /
Array not working as expected
I want to make a save/load system and it's pretty much done. The last thing I want to add si to return objects to their startPosition when the scene is reloaded. The code below sets the startPosition value:
public Transform startPosition;
void Start () {
startPosition = FindObjectOfType().stuffParent.transform;
Vector2 position = new Vector2(transform.position.x, transform.position.y);
startPosition.position = position;
}
This runs when a the same scene is being loaded but for some reason it sets all the enemies to the startPosition of 1 enemy:
if (FindObjectsOfType().Length != 0) {
foreach (Enemy enemy in FindObjectsOfType())
{
enemy.transform.position = enemy.startPosition.position;
}
for (int i = 0; i < FindObjectsOfType<Enemy>().Length; i++)
{
FindObjectsOfType<Enemy>()[i].transform.position = FindObjectsOfType<Enemy>()[i].startPosition.position;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612211532im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# declaring an array of transform arrays 1 Answer
How to check for an empty array? 1 Answer
Optimize the code (Array Unity) 1 Answer