- Home /
Why does object movement get saved to scene editor?
I have objects at specific locations in my scene. When I play the scene those objects move as expected. However, when I stop play and return to the scene editor, the objects are now in whatever position they ended in. Why would it be that way? Shouldn't the scene objects reset to where I put them in the editor?
foreach (GameObject enemyObject in GameObject.FindGameObjectsWithTag("Enemy"))
{
Vector3 startPos = enemyObject.transform.position;
Enemy enemy = enemyObject.GetComponent<Enemy>();
enemy.path.Add(startPos);
enemyObject.transform.position = (startPos.normalized * 10.0f) + startPos;
}
It basically disperses the enemies then sets a path for them to return to their starting position.
The objects should reset, and I cannot think of a scenario rather than a bug in Unity that would cause them to save their moved position.
Yeah, I'm at a loss. It's almost funny. With each run the objects in the scene are beco$$anonymous$$g more dispersed. Sometimes it seems like they go back to where they were last time, but it's very hard to tell at this point. I guess I'll keep hitting it with a hammer until it works.
Answer by nicolasjr · Apr 11, 2014 at 06:29 PM
That's weird. The only possibility I can think for that to happen is if you are handling not the gameobject, but the prefab; that'd explain the objects keep the final "play" position.
But it sure doesn't looks like that, based on your foreach. If you're running this code in a "enemies parent", a transform that contains all the enemies, you can try to replace your code with something like:
foreach(Enemy enemy in GetComponents<Enemy>())
{
//Do your thing here
}
Let me know if it helped!
Answer by reallybadgame · Apr 12, 2014 at 01:18 AM
OK, I'm pretty sure the cause was a leftover ExecuteInEditMode attribute in a base class that also happened to be completely unnecessary. It was executing the Start method in the editor before it executed it in the player, thus doubling up the initial movement and applying it to the editor. Once the base class was removed it works fine. Thanks for the suggestions though. Stuff like this makes me think I'm going crazy.