- Home /
"Object reference not set to an instance of an object" after LoadScene
Hello! I believe I may have asked this in the wrong place before so I'll post it here.
This is my first time posting here and I'm not entirely familiar with everything, but I'll do my best in explaining my issue. I'm not entirely unfamiliar with C# but I'm new to Unity all together. I'm trying to create an incremental game and currently I've got two scenes up. One scene is a simple "Start game." scene with a button to start the game. The code for that is
[code=CSharp]using UnityEngine; using System.Collections; using UnityEngine.SceneManagement;
public class StartGame : MonoBehaviour {
public void ChangeScene(string sceneName)
{
SceneManager.LoadScene("Test");
}
} [/code]
That brings us to the second scene which has the issue. There are two buttons which are meant to switch between two panels. The two panels are called "HeroHire" and "ItemPanel". The code that both buttons have to switch the two panels is
[code=CSharp]using UnityEngine; using System.Collections; using UnityEngine.UI;
public class HeroTabClick : MonoBehaviour { public GameObject HeroHire; public GameObject ItemPanel;
void Start()
{
HeroHire = GameObject.FindGameObjectWithTag("HeroHire");
ItemPanel = GameObject.FindGameObjectWithTag("ItemPanel");
}
public void Test()
{
HeroHire.SetActive(true);
ItemPanel.SetActive(false);
}
} [/code]
[code=CSharp]using UnityEngine; using System.Collections; using UnityEngine.UI;
public class ItemTabClick : MonoBehaviour { public GameObject HeroHire; public GameObject ItemPanel;
void Start()
{
HeroHire = GameObject.FindGameObjectWithTag("HeroHire");
ItemPanel = GameObject.FindGameObjectWithTag("ItemPanel");
}
public void Test()
{
HeroHire.SetActive(false);
ItemPanel.SetActive(true);
}
} [/code]
Now onto my issue. If I run the second scene by itself and click the two buttons to switch the panels everything works fine, but if I go through the start scene I get the following errors,
"NullReferenceException: Object reference not set to an instance of an object HeroTabClick.Test () (at Assets/Scripts/HeroTabClick.cs:18)" and "NullReferenceException: Object reference not set to an instance of an object ItemTabClick.Test () (at Assets/Scripts/ItemTabClick.cs:18)"
I switch to the HeroTab panel just fine, but trying to switch to the ItemTab panel just gives me a blank result and that error, and both errors point to ItemPanel.SetActive(false);
I'm very confused since I don't know why it runs perfectly with no errors if I run that scene explicitly but if I run the start scene first it doesn't. Also, why is it only the ItemPanel that is erroring and how do I fix this?
Hopefully I gave you guys enough information and I'll update as soon as I can if you need more information.
Thank you in advance!
Your answer
![](https://koobas.hobune.stream/wayback/20220612075619im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a Panel (Or Scrollbar) Appear on Button Click 0 Answers
How do I get the width and height of text then apply it to a UI panel? 0 Answers
Add Text To UI Panel on Button Click 1 Answer
Make panel inactive on second click 1 Answer
How can i scale a ui Panel from the left side only ? 2 Answers