DontDestroyOnLoad resets my values

I did a simple test - here is my code:

public class test : MonoBehaviour {

    bool testedValue;


    void Awake () {
        Debug.Log("tested value on load:" + testedValue);
        DontDestroyOnLoad(this.gameObject);
    }
     // Use this for initialization
    void Start () {
        testedValue = true;
    }
   
    void Restart () {
        Application.LoadLevel(Application.loadedLevel);
    }
}

And always after restarting level my bool value is false. From my understanding, DontDestroyOnLoad shouldnt reset my value, so why it is happening?

1 Like

thats weird. Is the gameobject that Test is attached to in the scene. if so its probably loading it again with the scene so you end up with two. That may cause some weird behaviour. Check your scene for multiple copies of the game object when running restart function.

Thank You for answer!

So I have added some code to make sure it is single, and I have unfortunatly still exacly same results…

bool testedValue;


    public static test tested;
  
    void Awake (){
        Debug.Log("tested value on load:" + testedValue);


        if (tested == null)
        {
            DontDestroyOnLoad(gameObject);
            tested = this;
        }
        else if (tested != this)
        {
            Destroy(gameObject);
        }
    }


     // Use this for initialization
    void Start () {
        testedValue = true;
    }
  
    void Restart () {
        Application.LoadLevel(Application.loadedLevel);
    }

I just created new project, with only one gameobject, canvas and restart button. On gameobject I`ve attached this script, assigned button to restart function, and tested it - still this GO is set to false on restart…

I have managed to workaround by saveing bool in file, but it isn`t very good in futher perspective, any idea what can be wrong?

try loading a different empty scene and see if you still get the bool resetting problem.

the bool is private so are you using the debug in awake for verificaiton. if so when you reload the scene the new gameobject that was loaded with the scene is debugging it as false because it runs before it is set to true in start.
make the bool public and check it in the inspector.

1 Like

Thank You! With this idea I managed to solve this mistery… Althought answer is a little different - Awake is called only on creation, so putting debug in awake was called only for new instances which always had false value. So even when there is single instance check - debug was before this check, so it displayed value for instantly after destroyed GO. And everything in start in right GO wasn`t runned because there is no start in loading level with dontdestroyonload.

Now I have next dillema because of this behavior - I have to use OnLevelWasLoaded - but it is called before Awake(!), so I always get two answers - one for GO which is from previous scene, and second which is created in this scene, but isnt yet destroyed. Any if statement I will use, still be affected by this zombie GO which just wont die in time :wink: Maybe You have any sugestions?

I have one workaround - to start coroutine in onlevelwasloade with one yield return null, but it is little dangerous as I use it already for other things which I couldnt put in start, because it was too early for other things in other starts GO (as I had read about start sequence, You can`t be ever sure which GO will run first code in this section)

instead of having it in the scene by default use a script to instantiate a prefab of the gameobject on start up of the scene if no other gameobjects of the same exist. example using a tag to look.

void Start()
    {
        if(!GameObject.FindGameObjectWithTag("This tag"))
        {
            Instantiate(prefab);
        }
    }

that way it will only exist if another gameobject with the same tag doesnt already exist.

Nice thought, but when there are two of this objects, both wont do anything (in case of putting this code in onlevelwasloaded), and on start will work only on new one, second wont call this method as it isn`t created in new scene (so I need to use OnLevelWasLoaded). Anyway, now I will figure something out, many thanks!