Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Problem with DontDestroyOnLoad objects when loading scene 0

Discussion in 'Scripting' started by SpartianTheDev, Apr 17, 2020.

  1. SpartianTheDev

    SpartianTheDev

    Joined:
    Jan 17, 2019
    Posts:
    11
    Scene 0 is main menu and when I load main menu (It is only happening on scene 0) I got errors about missing referances. I debugged with OnDestroy() and saw it is destroying object but that variables is visible in Inspector/Debug mode and they are not null or missing. Weird!

    I tested a bit more and error was given for transform too. The error says "The object of type <scriptName> has been destroyed. You should check either if it is null or you should not destroy the object." For transform.GetComponent<>!!

    Note: DontDestroyOnLoad object is a root object.

    I use fade transition effect with a script so transition method:

    Code (CSharp):
    1. public IEnumerator SceneTransition(int levelId)
    2. {
    3.     myTween.Kill();
    4.     myTween = _canvasGroup.DOFade(1f, 1f);
    5.     myTween.SetEase(Ease.Linear);
    6.     yield return myTween.WaitForCompletion();
    7.     //
    8.     SceneManager.LoadScene(levelId);
    9.     //
    10.     myTween = _canvasGroup.DOFade(0f, 1f);
    11.     yield return myTween.WaitForCompletion();
    12. }
    13.  
    Is there an exception for scene0 for DontDestroyOnLoad objects?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    It's almost impossible to know what you mean because we can't see your setup. But by "scene 0" you mean the first scene, if your singletons are setup correctly, there should never be an issue with your dontdestroyonload objects.

    Also, another way to work around it is to have a preload scene where those objects are created and then you never go back to that scene.

    And of course, the third option is to make sure your dontdestroyonload objects are necessary.

    If you're not certain what is happening, the simple answer is you're getting duplicate objects because your object exist in the scene, then you use dontdestroyonload to carry it around so when you get back to the scene you now have a double.
     
    SpartianTheDev and eses like this.
  3. SpartianTheDev

    SpartianTheDev

    Joined:
    Jan 17, 2019
    Posts:
    11
    I really thought this but if I cant find any solution i will do this.


    I will check that but how can get a missing reference error for calling transform?
     
  4. SpartianTheDev

    SpartianTheDev

    Joined:
    Jan 17, 2019
    Posts:
    11
    Oh I understand.
    The manager scripts are on the scene0. when i call scene0, it is duplicating. The scene0 set-up and the dontdestroyonload one.
    Thanks for reply:p