Activate and deactivate panels with buttons
I have 4 buttons, and I want each of then to activate and deactivate a different panel. this is what my script looks like:
public class Copy : MonoBehaviour
{
GameObject ThorPanel, TyrPanel, SkadiPanel, HelPanel;
// Use this for initialization
void Start()
{
ThorPanel = GameObject.Find("Thor Panel"); ThorPanel.SetActive(false);
TyrPanel = GameObject.Find("Tyr Panel"); TyrPanel.SetActive(false);
SkadiPanel = GameObject.Find("Skadi Panel"); SkadiPanel.SetActive(false);
HelPanel = GameObject.Find("Hel Panel"); HelPanel.SetActive(false);
}
public void Thor()
{
Debug.Log("Thor selected");
ThorPanel.SetActive(true);
}
public void Tyr()
{
Debug.Log("Tyr selected");
TyrPanel.SetActive(true);
}
public void Skadi()
{
Debug.Log("Skadi selected");
SkadiPanel.SetActive(true);
}
public void Hel()
{
Debug.Log("Hel selected");
HelPanel.SetActive(true);
}
}
The text is printed to the console, but the panels wont activate(except for thor's panel for some reason). I get this error message on the start for all panels except thor: NullReferenceException: Object reference not set to an instance of an object. When I try to activate a panel by pressing the button, the same message shows up. The only difference between thor's panels to the rest is that thor's panel has two images and texts set as children.
This seems like something that should be simple but it's not working for me, and I really don't get why one panel works but the others don't when they're almost the same. Any help would be appreciated ^^
EDIT 12.04: Thanks to the help from @unity_124ggn and @Vladislav-Videnov, I figured out why I was getting a null reference: the script tried to run the start function four different times, because it was placed on four different objects. After the start function worked once, the rest couldn't find the deactivated object. I fixed that by adding a new script:
public class Startup : MonoBehaviour {
public static GameObject ThorPanel, TyrPanel, SkadiPanel, HelPanel;
void Start ()
{
ThorPanel = GameObject.Find("ThorPanel"); ThorPanel.SetActive(false);
TyrPanel = GameObject.Find("TyrPanel"); TyrPanel.SetActive(false);
SkadiPanel = GameObject.Find("SkadiPanel"); SkadiPanel.SetActive(false);
HelPanel = GameObject.Find("HelPanel"); HelPanel.SetActive(false);
}
}
The problem I have now is that the panels wont deactivate when I press a different button. The updated script looks like this:
public class Copy : MonoBehaviour
{
bool tyr, thor, skadi, hel;
//each of the four buttons holds a singke, different function
public void Thor()
{
Debug.Log("Thor selected");
thor = true;
Startup.ThorPanel.SetActive(true);
//This part should repeat for each function, I only placed it once for testing
if(tyr)
{
tyr = false;
Startup.TyrPanel.SetActive(false);
}
if(skadi)
{
skadi = false;
Startup.SkadiPanel.SetActive(false);
}
if(hel)
{
hel = false;
Startup.HelPanel.SetActive(false);
}
}
public void Tyr()
{
Debug.Log("Tyr selected");
tyr = true;
Startup.TyrPanel.SetActive(true);
}
public void Skadi()
{
Debug.Log("Skadi selected");
skadi = true;
Startup.SkadiPanel.SetActive(true);
}
public void Hel()
{
Debug.Log("Hel selected");
hel = true;
Startup.HelPanel.SetActive(true);
}
}
Once aagin, any help would be appreciated ^^
Answer by unity_124ggn · Apr 11, 2019 at 08:19 PM
Quick question, how are the panels activated in the first place?
The default is that all panels are active at the same time, so I'm trying to deactivate them all at the start, and then activate each one only when I'm clicking it's button
Well that's a little odd. If you use GameObject.SetActive(false); you will no longer be able to use the button. The GameObject isn't on. $$anonymous$$aybe try putting a button over it that turns it on?
Answer by Vladislav-Videnov · Apr 11, 2019 at 09:35 PM
@Lineweaver Make sure the buttons are on separate game objects from the ones you are enabling / disabling. Otherwise if you disable your object with button , you wont be able to click it.
If the behavior works for the rest, but specifically not for one, then check for spelling mistakes. GameObject.Find("object name") is often failing due to that. The error seems like your object is not found, that's why you get a null reference exception.
Answer by KittyAnn · Apr 13, 2019 at 07:21 AM
@Lineweaver I don't know if you've figured this out or not, but here's my "Pause Game Menu" with many different panels on it. I made an "Empty Game Object" in the canvas & named it PauseGameMenu and attached this script to another empty game object ( I named mine Global Stats Objects" and assign all your variables in it. I hope this helps you figure your script out. P.S. You start with ONLY the empty game object ACTIVE in the canvas the "PublicGameObject", not the child panels (unless you need one set active upon game start). The script will turn on & off the different panels.
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseGameMenu : MonoBehaviour
{
[Header("Main Game Music")]
public AudioSource mainGameMusic;
[Header("Pause Menu Music")]
public AudioSource pauseMenuMusic;
[Header("ButtonPressSound")]
public AudioSource pressButtonSound;
[Header("Is the PauseGameMenu Open?")]
public bool pauseGameMenu = false;
[Header("PauseGameMenu in Canvas")]
public GameObject pauseMenu;
[Header("Turn OFF the Player")]
public GameObject thePlayer;
[Header("Player Name Display Off")]
public GameObject playerNameDisplay;
[Header("Turn OFF Save Position")]
public GameObject savePositionButton;
[Header("Turn OFF Load Position")]
public GameObject loadPositionButton;
[Header("Items Panel in Canvas")]
public GameObject itemPanel;
[Header("House Items Panel in Canvas")]
public GameObject itemHousePanel;
[Header("Pet Items Panel in Canvas")]
public GameObject itemPetPanel;
[Header("Player Items Panel in Canvas")]
public GameObject itemPlayerPanel;
[Header("Current Quests Panel in Canvas")]
public GameObject currentQuestsPanel;
[Header("All Quests Panel in Canvas")]
public GameObject allQuestsPanel;
[Header("Q001 Info Button in Canvas")]
public GameObject Q001PickFlowerSwordButton;
[Header("Q002 Info Panel in Canvas")]
public GameObject Q002KillTheSpidersButton;
[Header("Q003 Info Panel in Canvas")]
public GameObject Q003CatchHurtButterflyButton;
[Header("Q001 Info Panel in Canvas")]
public GameObject Q001InfoPanel;
[Header("Q002 Info Panel in Canvas")]
public GameObject Q002InfoPanel;
[Header("Q003 Info Panel in Canvas")]
public GameObject Q003InfoPanel;
[Header("Stats Panel in Canvas")]
public GameObject statPanel;
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Cancel"))
{
if (pauseGameMenu == false)
{
pressButtonSound.Play();
pauseMenuMusic.Play();
mainGameMusic.Pause();
Time.timeScale = 0;
pauseGameMenu = true;
Cursor.visible = true;
pauseMenu.SetActive(true);
thePlayer.GetComponent<CubeMovement>().enabled = false;
playerNameDisplay.SetActive(false);
savePositionButton.SetActive(false);
loadPositionButton.SetActive(false);
}
else
{
pauseMenu.SetActive(false);
mainGameMusic.UnPause();
pauseMenuMusic.Pause();
Cursor.visible = true;
pauseGameMenu = false;
Time.timeScale = 1;
thePlayer.GetComponent<CubeMovement>().enabled = true;
playerNameDisplay.SetActive(true);
savePositionButton.SetActive(true);
loadPositionButton.SetActive(true);
}
}
}
// All Buttons Go Here
//Main "Items Button" on far left
public void ShowItem()
{
itemPanel.SetActive(true);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
//House Items Button within the Main Items Button
public void ShowHouseItems()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(true);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
//Pet Items Button within the Main Items Button
public void ShowPetItems()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(true);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
//Player Items Button within the Main Items Button
public void ShowPlayerItems()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(true);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
public void ShowCurrentQuest()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(true);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
public void ShowAllQuestsList()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(true);
Q001PickFlowerSwordButton.SetActive(true);
Q002KillTheSpidersButton.SetActive(true);
Q003CatchHurtButterflyButton.SetActive(true);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
public void ShowQ001InfoPanel()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(true);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
public void ShowQ002InfoPanel()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(true);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(false);
}
public void ShowQ003InfoPanel()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001PickFlowerSwordButton.SetActive(false);
Q002KillTheSpidersButton.SetActive(false);
Q003CatchHurtButterflyButton.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(true);
statPanel.SetActive(false);
}
public void ShowStat()
{
itemPanel.SetActive(false);
itemHousePanel.SetActive(false);
itemPetPanel.SetActive(false);
itemPlayerPanel.SetActive(false);
currentQuestsPanel.SetActive(false);
allQuestsPanel.SetActive(false);
Q001InfoPanel.SetActive(false);
Q002InfoPanel.SetActive(false);
Q003InfoPanel.SetActive(false);
statPanel.SetActive(true);
}
public void ResumeGame()
{
pauseMenu.SetActive(false);
pressButtonSound.Play();
pauseMenuMusic.Pause();
mainGameMusic.UnPause();
Cursor.visible = true;
pauseGameMenu = false;
Time.timeScale = 1;
thePlayer.GetComponent<CubeMovement>().enabled = true;
playerNameDisplay.SetActive(true);
savePositionButton.SetActive(true);
loadPositionButton.SetActive(true);
}
public void RestartLevel()
{
pauseMenu.SetActive(false);
pressButtonSound.Play();
Cursor.visible = true;
pauseGameMenu = false;
Time.timeScale = 1;
thePlayer.GetComponent<CubeMovement>().enabled = true;
playerNameDisplay.SetActive(true);
savePositionButton.SetActive(true);
loadPositionButton.SetActive(true);
SceneManager.LoadScene(1);
}
public void QuitToMenu()
{
pauseMenuMusic.Pause();
pauseMenu.SetActive(false);
pressButtonSound.Play();
Cursor.visible = true;
pauseGameMenu = false;
Time.timeScale = 1;
thePlayer.GetComponent<CubeMovement>().enabled = true;
playerNameDisplay.SetActive(true);
savePositionButton.SetActive(true);
loadPositionButton.SetActive(true);
SceneManager.LoadScene(0);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612195304im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move UI panel on and off screen, scripted without animation 0 Answers
ObjectReference Not set to an instance of an object 1 Answer
How to enable a panel after collision 1 Answer
I want to Automatically resize/snap buttons to my panel but it isnt working 0 Answers
Screen Format Question (Panels) 1 Answer