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. Dismiss Notice

Resolved How to access a game object in another scene?

Discussion in 'Scripting' started by lavagoatGG, Jul 6, 2023.

  1. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    218
    In my game, If an error occures, I want to move the player to the menu scene and in that scene display an error message. My idea was to have a script with a static bool variable on the menu scene so at the Start() function if the variable is true it will display the message. How do I cange this variable from a script in another scene?
    Getting a refrance to the object in the menu scene is not an option.

    Thank you!
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    But only if both scenes are loaded together. If you want to pass information from one scene to another after replaced the scene, you can store information in static variables or your can have DontDestroyOnLoad objects for this specific purpose. I like the static variables more. I usually build a kind of blackboard system around this so it can be used for all kind of information I need to store for gameplay.
     
  3. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    218
    How can you use static vars in this case? Can you give me an example?
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    Static Variables are great, but can also cause spaghetti code for larger projects, since they tend to make things less modular. Static variables are bound to the class itself, so you can just write MyClass.myStaticVariable = myValue.

    Anyways.. "if there is an error"? As in an actual exception? I wouldnt spend effort on making sure the player ends up in the main menu if an exception occurs. How do you intend to accomplish that? The player will be annoyed anyways, whether the game crashes or he ends up in the main menu makes little difference imho. Rather make sure there is no exceptions to begin with.
     
    Chubzdoomer and lavagoatGG like this.
  5. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    218
    It's a multiplayer so if the host suddenly disscontcts. Thanks for the help.
     
    Yoreki likes this.
  6. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,104
    You can use a scriptable object asset to allow objects in different scenes to communicate with each other.

    The scriptable object:
    Code (CSharp):
    1. [CreateAssetMenu]
    2. public sealed class Error : ScriptableObject
    3. {
    4.     public event Action<string> Occurred;
    5.  
    6.     public string Message { get; private set; } = "";
    7.  
    8.     public void Report(string message)
    9.     {
    10.         if(string.IsNullOrEmpty(message))
    11.         {
    12.             Message = "";
    13.             return;
    14.         }
    15.      
    16.         Message = message;
    17.         Occurred?.Invoke(message);
    18.     }
    19.  
    20.     public void Clear() => Message = "";
    21.  
    22.     private void OnEnable() => EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
    23.     private void OnDisable() => EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
    24.     private void OnPlayModeStateChanged(PlayModeStateChange mode) => Message = "";
    25. }
    How to use it:
    Code (CSharp):
    1. public sealed class ErrorReporter : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     privates string message;
    5.  
    6.     [SerializeField]
    7.     private Error error;
    8.  
    9.     public void Report() => error.Report(message);
    10. }
    Code (CSharp):
    1. public sealed class OnErrorLoadScene : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private Error error;
    5.  
    6.     [SerializeField]
    7.     private string scene;
    8.  
    9.     private void OnEnable() => error.Occurred += OnErrorOccurred;
    10.     private void OnDisable() => error.Occurred -= OnErrorOccurred;
    11.  
    12.     private void OnErrorOccurred(string message) => SceneManager.LoadScene(scene);
    13. }
    Code (CSharp):
    1. public sealed class ErrorDisplayer : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private Error error;
    5.  
    6.     [SerializeField]
    7.     private TMP_Text displayer;
    8.  
    9.     private void Start()
    10.     {
    11.         if(string.IsNullOrEmpty(error.Message))
    12.         {
    13.             return;
    14.         }
    15.      
    16.         displayer.text = error.Message;
    17.         error.Clear();
    18.     }
    19. }
     
    Chubzdoomer and lavagoatGG like this.