Search Unity

Unable to reference a gameobject from a script.

Discussion in 'Scripting' started by Wrenger, May 7, 2020.

  1. Wrenger

    Wrenger

    Joined:
    Apr 28, 2020
    Posts:
    71
    I have two scenes Main and Game (both of them under a new empty containing the whole scene.
    When i click play i want the gameobject (tagged "AllScene") to turn off so i cant see the MainScene.
    Code (CSharp):
    1.  
    2. public class MenuScript : MonoBehaviour
    3. {
    4.     public void Play()
    5.     {
    6.         SceneManager.LoadScene("GameSceneLight",LoadSceneMode.Additive);
    7.         MenuScene.SetActive(false);
    8.     }
    9.     }
    When I click back from the game scene, I want the MainScene to turn back on, and the game scene (tagged "GameScene") to delete itself.

    Code (CSharp):
    1. public class GameOver : MonoBehaviour
    2. {
    3.     public GameObject GameScene;
    4.     public GameObject MenuScene;
    5.     void Start()
    6.     {
    7.         MenuScene = GameObject.FindGameObjectWithTag("AllScene");
    8. }
    9.     public void BackToMenuFromGame ()
    10.     {
    11.         GameScene.SetActive(false);
    12.         MenuScene.SetActive(true);
    13.     }
    14. }
    But the second part won't work.
    upload_2020-5-7_23-21-44.png
    Clearly, the No cameras rendering showing up is because the Main scene won't load, meaning no scenes, no cameras, are available.
    Btw is my 4th day since started learning c# & unity. No need to say im stupid, I know :)
     

    Attached Files:

  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The scene is loaded - you can see it in the Hierarchy panel - but it's inactive, hence the No camera rendering message.

    Clearly there's more to these scripts (given the error is on line 42 of a 14 line paste), and what's missing is important. Most importantly, how and when is GameScene being set?
     
    matkoniecz likes this.
  3. Wrenger

    Wrenger

    Joined:
    Apr 28, 2020
    Posts:
    71
    Yeah there is more to it, but that code is useless. The error i get on line 42 is this error. I shortened everything to the core because thats the only problem i have.
    When the game ends, GameOver is called, which has the role of finding the game object with the "AllScene" tag
    Code (CSharp):
    1.  
    2. [LIST=1]
    3. [*]    void Start()
    4. [*]    {
    5. [*]        MenuScene = GameObject.FindGameObjectWithTag("AllScene");
    6. [*]     }
    7. [/LIST]
    8. ;
    Which it does not, because when i call it,
    Code (CSharp):
    1.         public void BackToMenuFromGame ()
    2.         {
    3.             GameScene.SetActive(false);
    4.             MenuScene.SetActive(true);
    5.         }
    I get the error at line 42.
    The problem is, i just cant get the gameobject to be found during game by the tag.
     
    Last edited: May 8, 2020
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    That's where the error is, but that may not be where the problem is. In particular, I asked:
    Which I still don't see anywhere.

    Since you didn't post the full script I can't see your line numbers, this is absolutely meaningless to me.