- Home /
Unable to load position of objects with the Animator Component attached.
Hi. Sorry to have to ask a noob question but I'm really stuck and i cant figure this out by myself.
I'm messing around with making a 3d fps game in c# with Unity. I have a save script that uses serialisation to store the transform and rotation of my 3d object between sessions using F6 and F9. When i attach the save script to my object i can move it and save it and close the game and when i reload my save, the object is where i saved it. I want to use this for doors. So i set it up as a sliding door, added an animation in unity that moved it up on the Y axis. When i save with the door in the air and load. The door is back down again and i cant get it to stay up on load. I've read so many forum posts but i cant find one with the answer that works for me. I can paste my code in here if it helps. It works without the animator attached but not with it. I spent 2 days already and its driving me mad. Sorry for the poor formatting, i don't make forum posts very much.
I have the latest build of Unity and Visual Studio.
Thank you for your time.
Answer by viesc123 · May 29, 2020 at 04:40 PM
In the Unity execution order, the animation update comes after the setting of the position in C# Update and will therefore overwrite anything you set in code, every frame. Animator works like this: If you have an animated property (say the position or rotation of the door object) in any state of your animation, even if the current state does not contain keyframes for that property, it will be overwritten with the last set keyframe or some default value as long the Animator is active. So, you can read the position of your door any time, but you can only set it, if the Animator component is deactivated. Alternatively, your save system could store the animation state the door is in, when it's open an set that when you load.
Yes, THIS! Thank you so much. I had tried this once before but thought i was going in the wrong direction. I added a bool to my door script that says if the door is open or closed. When the door is open, it sets the bool to true and i saved the bools state in my data script. When i load it checks the bool and if the door was open or closed on save it executes accordingly. Thank you again.
Answer by N-8-D-e-v · May 29, 2020 at 03:44 PM
In your animation you are probably editing the position of your Game Object. Click on the door, go to the animation window, and delete the "position property". I've had this problem in the past, this fixed it.