Search Unity

Newb question - How to assign variable with gameobject in other scene.

Discussion in 'Scripting' started by quillbolt, Apr 19, 2019.

  1. quillbolt

    quillbolt

    Joined:
    Apr 23, 2017
    Posts:
    2
    Hi. I've been following along with "Coding with Ejay"'s tutorial on how to make a 2D RPG. However, I've run into a problem with my Game controller and Health Bar, which I've only been able to partly fix.

    My Error is : "UnassignedReferenceException: The variable PlayerHealthBarT of GameController has not been assigned".

    This error only shows up when starting the game from the World scene, rather than the Battle Scene. This is because the Health Bar that I assign to HealthBarT is only present in the Battle scene. I seemingly can't assign it from the World scene.

    I found a partial fix that allowed the feature to work-

    Code (CSharp):
    1. private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    2.     {
    3.         if (scene.name == "Game" && playerDead == true)
    4.         {
    5.             inBattle = false;
    6.  
    7.             Destroy (GameObject.Find ("Enemy"));
    8.  
    9.         }
    10.         if (scene.name == "Battle")
    11.         {
    12.             inBattle = true;
    13.  
    14.             Debug.Log("BATTLE");
    15.            
    16.             GameObject gameObject = GameObject.Find("PlayerHealthBar");
    17.             if (gameObject != null)
    18.             {
    19.                 Debug.Log("HealthBar Object Present =" + gameObject.name);
    20.                 PlayerHealthBarT = gameObject.GetComponent<RectTransform>();
    21.                 PlayerHealthBar = gameObject;
    22.                  
    23.             }
    24.             else if (control == null)
    25.             {
    26.                 Debug.Log("HealthBar Object Not Present");
    27.             }
    28.            
    29.         }
    30.  
    31.     }
    But while this assigns the HealthBar to HealthBarT on the battle scenes loading and allows the feature to function, it doesn't stop the error from spamming my console until the player enters the battle scene.

    Is there a way to do this from void start() ? Or is there a more elegant way to do it? While it isnt game breaking I somehow feel that leaving errors doesn't make for good practice.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You can't make references to objects that exist in scenes that aren't currently loaded. There are various ways of dealing with this, from loading multiple scenes at the same time and just disabling GameObjects you don't want in the second scene until needed, to moving GameObjects you need in a later scene to the first scene and setting DontDestroyOnLoad, to establishing references to second scene objects once that scene is loaded, to making the object a prefab instead of a scene object so you can instantiate it in any scene in which you need it.

    What you can't do is design your game to where it needs to set a reference to an object that only exists in another scene that for other reasons you don't want loaded at that time. You need to design this system in another way if that is what you think you need.
     
    quillbolt likes this.