- Home /
Help with loading scenes
When I press the continue button on my main menu a script loads in and reads a file from the disk that stores the players position. When the script is loaded the script does what it's supposed to do and places the player in the position specified within the file.
However, after a second the player object will revert back to the position it is in the editor.
Is there something specific I have to do with the order of these things?
Is there a second player object in the new scene you are loading?
Answer by kingcoyote · Oct 12, 2016 at 10:49 PM
The most common culprit for data reverting back to an older state, or to a null/zero state, when the game starts, stops, or scripts are reloaded, is using variables that are not serializable by Unity.
When the Unity editor starts, it takes all GameObjects and serializes them, recreates them, and reapplies the serialized data. When the editor stops, the GameObjects are all cleared out and the serialized data is restored. This is to keep your game state as intended and to prevent playing the game from causing undesirable changes. Unity also does this serialize/recreate sequence if a script is reloaded during the game.
You can check to see what variables are serializable by Unity and try to stick to those value types, especially on public fields. I ran into issues with some GameObject data constantly being reset only to find that I was using a Dictionary, and that is not serializable. I ended up having to use a List that is converted to a Dictionary on Start, using one of the struct's properties as a key.
I don't think that's the problem as I'm using a Vector3 and Quaternion which are definitely serializable. I set up another project and imported the same scripts and all seems to be working well there.
I did rewrite the way data was shared between scene change so maybe that was the issue. Thanks for the information, though.
Answer by LZakhar · Oct 12, 2016 at 08:54 PM
Is there any default variables that can be set at/after loading process and somewhere on Update/FixedUpdete/LateUpdate etc. Position of your player resets to them? Silly but there ara so many little rocks. No way wanted to injure you. Did same mistakes by myself many times, and much more time then left to find and debug...
Nah, I've looked a couple of times just in case. I can't see anything in my code that would revert the player position.
And so? the problem is still there? $$anonymous$$aybe some pice of code would help, but I don`t even emagine nedded place. Load/Start level? In my game levels loading from SQLite too, but all seems to be on their places.
And foolish question is loading going to game or to editor? For me as example there is some tools writen exclusive for editor, for easly level makenink, etc. If it is an Editor script and you dond use on DrawDefaultInspector() func OnInspectorGUI you need to use EditorUtility.SetDirty func. I remembered. In that hole I was once.
It was a problem with how my code was executing in Unity. I wrote a new script with more thought out logic. Thanks for trying to help :)
Your answer
Follow this Question
Related Questions
Scene loader Toutorial 0 Answers
Function deleting string by itself. 0 Answers
Loading screen with long-running CPU-heavy Awake() 1 Answer
Problems with Playerprefs script 1 Answer