Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

some gameobjects don't destroy between scenes using Application.LoadLevel

Discussion in 'Scripting' started by julienrobert, Oct 13, 2017.

  1. julienrobert

    julienrobert

    Joined:
    Sep 15, 2014
    Posts:
    65
    I'm using 5.2.5 and using Application.LoadLevel to change my scenes.

    I don't understand why a gameobject having some script and GUIText components stays into the next scene. That script has 1880 lines so I won't paste it here. I don't use dontDestroyOnLoad inside it and I don't have static variable.
     
  2. MaxGuernseyIII

    MaxGuernseyIII

    Joined:
    Aug 23, 2015
    Posts:
    315
    You should probably start by pouring a circle of salt around your computer. :)

    Are you doing an additive load or something? I don't even know if that was a thing back in 5.2.
     
  3. julienrobert

    julienrobert

    Joined:
    Sep 15, 2014
    Posts:
    65
    eheh. I can't update because one plugin breaks in newer versions.

    I forgot one script component on that game object uses some public static variable. That might be the problem?
     
  4. MaxGuernseyIII

    MaxGuernseyIII

    Joined:
    Aug 23, 2015
    Posts:
    315
    I haven't seen a public static variable cause that problem but, again, I haven't used 5.2.

    It's a long class with, it sounds like, coupling to other parts of the system. What is your confidence level that DontDestroyOnLoad really is not involved?
     
  5. julienrobert

    julienrobert

    Joined:
    Sep 15, 2014
    Posts:
    65
    yeah, I've found one (on a script I didn't write myself). But it's not that new gameobject "spout" that stays, it's the gameobject on which the script component is attached. Here is the part:

    Code (CSharp):
    1.         public static Spout instance {
    2.             get {
    3.                 if(_instance == null){
    4.                     _instance = GameObject.FindObjectOfType<Spout>();
    5.                     if(_instance == null) {
    6.                         GameObject _go = new GameObject("Spout");          
    7.                         _instance = _go.AddComponent<Spout>();
    8.                     }
    9.                     DontDestroyOnLoad(_instance.gameObject);
    10.                 }
    11.                 return _instance;
    12.             }
    13.             private set{_instance = value;}
    14.         }
     
  6. julienrobert

    julienrobert

    Joined:
    Sep 15, 2014
    Posts:
    65
    Ok, found a workaround. I leave the problematic script component alone on a gameobject that stays through the scene changes and it's good for me.