0
\$\begingroup\$

I have 2 scenes in my project, MainMenu and Game.

Main Menu Image

Game Image

MainMenu scene contains 2 buttons, 'Hide Black' and 'Hide White'. Game scene contains 2 panels, 'Black' and 'White'. When the user clicks any of button from main menu, I wanna hide the corresponding panel in Game scene and also load the scene. This is the script for the same.

using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenu : MonoBehaviour {
    public void HideBlack() {
        SceneManager.LoadScene(1);
        GameObject black = GameObject.Find("Black");
        black.SetActive(false);
    }

    public void HideWhite() {
        SceneManager.LoadScene(1);
        GameObject white = GameObject.Find("White");
        white.SetActive(false);
    }
}

But it turns out that GameObject.Find can't find the panels in my Game scene. I looked my forums for help but I find them confusing. What's going on here?

EDIT - This is not from my actual project. I created these scenes just to explain the problem better. I must use 2 scenes in my actual project as I have 3D objects in Game scenes.

\$\endgroup\$
6
  • \$\begingroup\$ It is because objects are in different scenes and AFAIK GameObject.Find searches only in current GameObject's scene. You should try looking at this unity answer for possible ideas. \$\endgroup\$
    – eLTomis
    Commented Jul 14, 2020 at 10:57
  • 1
    \$\begingroup\$ It's best not to look up game objects by name. I think you'd have an easier time if you put your panels in your menu scene and referenced them directly in the inspector. \$\endgroup\$
    – DMGregory
    Commented Jul 14, 2020 at 11:21
  • \$\begingroup\$ @DMGregory Do you mean I should you one scene only? I must use two scenes \$\endgroup\$ Commented Jul 14, 2020 at 17:51
  • \$\begingroup\$ You might want to explain the reason why in your question then. \$\endgroup\$
    – DMGregory
    Commented Jul 14, 2020 at 18:01
  • \$\begingroup\$ Okay, I just did. \$\endgroup\$ Commented Jul 14, 2020 at 18:09

1 Answer 1

0
\$\begingroup\$

I just figured in out. I wanted to load the same scene but with different UI on two different options. So I needed to hide other UI elements. To solve my problem I assigned Hide.cs script to an empty game object in my Game scene.

using UnityEngine;

public class Hide : MonoBehaviour {
    public GameObject black, white;
    public static bool hideBlack;

    void Start() {
        if(hideBlack) {
            black.SetActive(false);
        } else {
            white.SetActive(false);
        }
    }
}
 

And this is MainMenu.cs

using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenu : MonoBehaviour {
    public void HideBlack() {
        Hide.hideBlack = true;
        SceneManager.LoadScene(1);
    }

    public void HideWhite() {
        Hide.hideBlack = false;
        SceneManager.LoadScene(1);
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ It is generally not advised to use static fields for setting state of objects. It works for now but as your game progresses, it might introduce scaleability issues. What if you create third color? How will you select which ones to hide? \$\endgroup\$
    – eLTomis
    Commented Jul 15, 2020 at 13:51
  • \$\begingroup\$ Okay. I'll use interger instead of bool \$\endgroup\$ Commented Jul 15, 2020 at 19:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .