Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AsaNemu · Apr 26, 2016 at 04:26 AM · c#uipanel

"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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges