.Set Active () Object Reference Not Set to an Instance of an Object
So I'm getting an error on line 19 of this script and most of the buttons are not working and I'm not sure why. As I was adding to my UI (I was working on a settings tab) I somehow managed to break most of the buttons on my main menu and I cannot figure out why. Another thing I'd like to ask is if the GameObject.Find is the proper way to do this as it seems I'm doing this a lot in my scripts and I feel like I should be less dependent on that. Thanks in advance!
using UnityEngine;
using System.Collections;
public class MainMenu : MonoBehaviour {
private GameObject settingsMenu;
private GameObject mainMenu;
private Settings settingsScript;
private GameObject mainMenuManager;
void Awake () {
settingsMenu = GameObject.Find ("SettingsMenu");
mainMenuManager = GameObject.Find ("MainMenuManager");
settingsScript = mainMenuManager.GetComponent <Settings> ();
mainMenu = GameObject.Find ("MainMenu");
}
void Start () {
settingsMenu.SetActive (false);
settingsScript.enabled = false;
Time.timeScale = 1.0f;
}
public void NewGame() {
Application.LoadLevel (1);
}
public void QuitGame () {
Application.Quit ();
}
public void MainMenuSettings () {
settingsScript.enabled = true;
mainMenu.SetActive (false);
settingsMenu.SetActive (true);
}
public void GoBackToMainMenu() {
settingsMenu.SetActive (false);
mainMenu.SetActive (true);
}
}
EDIT: Wanted to clarify settings$$anonymous$$enu is a canvas in my scene.
Answer by ARKMs · Nov 05, 2015 at 08:11 AM
The problem is maybe 'settingsMenu' is null when you try use it. you can check this using something like:
void Start ()
{
if(settingsMenu == null) print("Is null u.u");
}
Is more efficient and easy way to get reference is use a public variable and set it in Editor.
Your answer
Follow this Question
Related Questions
Setting EventTrigger in Unity with JavaScript 0 Answers
Microsoft (R) Visual C# Compiler version 3.5.0-dev-20359-01 (8da8ba0c) error, 0 Answers
What am I doing wrong? 0 Answers
Toggle Button: hope it toggle once but it toggle many times 1 Answer
Locate _MainTex from a public shader and assign to instantiated prefab for GUI 1 Answer