Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can script from a scene find objects that are under DontDestroyOnLoad?

Discussion in 'Scripting' started by Robster95, Oct 2, 2020.

  1. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    I have a player and a camera system move from one scene to another. In the scene they're moving to, I am trying to have the gameobject call for the DontDestroyOnLoad gameobject that are being pushed into the scene.

    Will I be able to have the objects find and interact with each other? I cant figure out the right way to do it and i'm running into a roadblock with this problem.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yes you can find them. I usually give them a tag and use FindWithTag, and your camera probably already has the tag MainCamera. Though assigning themselves to a static variable in their Awake or Start methods and just checking if that variable is null or not is probably a better approach. But a few FindWithTag calls on scene load aren't a big deal.
     
    aravindshanmugam44 and Robster95 like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    If you have a script such as your "CameraSystem" that marks itself DontDestroyOnLoad(), it might also be helpful to simply write down its instance in a static variable within the CameraSystem class so you can easily find it.

    This is sort of a poor mans lazy manual singleton... I sprinkle them in quite often:

    Code (csharp):
    1.  public static CameraSystem Instance; { get; private set; }
    2.  
    3. void Awake()
    4. {
    5.   Instance = this;
    6. }
    Then anywhere can get at it with:

    Code (csharp):
    1. if (CameraSystem.Instance)
    2. {
    3.   CameraSystem.Instance.DoStuffForMePlease();
    4. }
    and when you're done just Destroy() the thing so you don't get two of them.
     
    Last edited: Oct 2, 2020
  4. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    I was actually able to get it. the problem I was having was that when I was trying to find the game object I was using gameObject instead of GameObject so I couldn't find the correct "find" method. However looking over your examples I noticed the get; private set; and was confused about that. I've been trying to learn more about unity and understand statics better now but i've been getting confused by the get set's. I know this is the wrong thread but would you mind helping me by expalining how those work and when they should be used?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    This means Instance can only be set from things inside the class, but can be get (read) by anyone.

    The idea is this class and ONLY this class should know when to set this variable, that's all.
     
  6. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    oh ok thank you!