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 Jayby18 · Aug 03, 2016 at 09:26 PM · c#nullreferenceexceptionvariablesuser interfaceobject-reference-error

How do I switch Canvases? & NullReferenceException (U5)

I am making a fps in C# and I want to have different canvases:

  1. HUD

  2. Pop Up Menu

  3. Pop Up Settings Menu

This is part of the ui script in a map:

 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 using UnityEngine.EventSystems;
 using System.Collections;
 using System.IO;
 
 public class userInterface : MonoBehaviour
 {
     public Canvas hud;
     public Canvas popUpMenu;
     public Canvas settingsMenu;
 
     void Start()
     {
         hud = hud.GetComponent<Canvas>();
         popUpMenu = popUpMenu.GetComponent<Canvas>();
         settingsMenu = settingsMenu.GetComponent<Canvas>();
 
         hud.enabled = true;
         popUpMenu.enabled = false;
         settingsMenu.enabled = false;
         Cursor.visible = false;
     }
 }

But then I get this error:

'Assets/Scripts/userInterface.cs(81,9): error CS0120: An object reference is required to access non-static member: userInterface.hud'

And this one:

'Assets/Scripts/userInterface.cs(82,9): error CS0120: An object reference is required to access non-static member userInterface.popUpMenu'

And this one:

'Assets/Scripts/userInterface.cs(83,9): error CS0120: An object reference is required to access non-static member userInterface.settingsMenu'

So I changed:

 public Canvas hud; 
 public Canvas popUpMenu;
 public Canvas settingsMenu;

Into:

 public static Canvas hud;
 public static Canvas popUpMenu;
 public static Canvas settingsMenu;

But then I get this error:

'NullReferenceException: Object reference not set to an instance of an object userInterface.Start () (at Assets/Scripts/userInterface.cs:19)'

And this one:

'NullReferenceException: Object reference not set to an instance of an object userInterface.Update () (at Assets/Scripts/userInterface.cs:31)'

And this one:

'NullReferenceException: Object reference not set to an instance of an object userInterface.Update () (at Assets/Scripts/userInterface.cs:31)'

But I can't assign the canvases to the variables, because static objects don't show up in the inspector. And when the variables were only public they didn't show up in the inspector either.

I want to switch canvases.

What do I do now?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Jayby18 · Aug 05, 2016 at 01:27 PM

I found a solution on my own:

I changed:

 public Canvas hud;
 public Canvas popUpMenu;
 public Canvas settingsMenu;

Into:

 public GameObject hud;
 public GameObject popUpMenu;
 public GameObject settingsMenu;

I removed the GetComponent lines.

And replaced:

 hud.enabled = true;
 popUpMenu = false;
 settingsMenu = false;

Into:

 hud.SetActive(true);
 popUpMenu.SetActive(false);
 settingsMenu.SetActive(false);

It worked completely fine.

Thanks for all your help though!

Comment
Add comment · Share
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
avatar image
0

Answer by XenonSpher · Aug 04, 2016 at 05:35 AM

NullReferenceException means you didn't put anything in the inspector view. so if it didn't show up in the inspector using "public Canvas hud". its probably bugged. so restart your unity. look at the object that contains the script. check your inspector again.

Comment
Add comment · Share
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
avatar image
0

Answer by lunoland · Aug 04, 2016 at 05:35 AM

You probably only need one canvas. Each of your UI elements should be attached to a child of the canvas you want them drawn on; If you want to switch those UI elements on and off, just enable and disable their objects. You can group several UI elements under an empty game object to control them as one.

If your userInterface script was attached to the same object as the canvas component, you would write hud = GetComponent<Canvas>(); to get a reference to it. This is shorthand for hud = this.GetComponent<Canvas>();, where the this keyword refers to the instance of your userInterface class. The error messages are a result of you calling hud.GetComponent instead.

Honestly it sounds like you're in a little over your head and you need to do some research first about GameObjects, Components, and organizing a scene in Unity, as well as the static keyword in C#. Out of scope for this question, but Google around and try out some of the tutorials on Unity's site and YouTube. Read the manual! https://docs.unity3d.com/Manual/index.html

Comment
Add comment · Share
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
avatar image
0

Answer by itsharshdeep · Aug 04, 2016 at 09:51 AM

Hi,

the Fast and easy way to switch in multiple is some thing like the following script:

 using UnityEngine;
 using System.Collections;
 
 public class UserInterface : MonoBehaviour
 {
 
     public enum CanvasStates {
         Hud,
         PopUp,
         Setting,
     }
 
     internal CanvasStates canvasState;
 
     [Header("Canvas")]
     [ToolTip("Place the hud canvas here")]
     public GameObject hud;
 
     [ToolTip("Place the popUpMenu canvas here")]
     public GameObject popUpMenu;
 
     [ToolTip("Place the settingsMenu canvas here")]
     public GameObject settingsMenu;
 
 
     // This method will remain active one cavas at a time just pass the state like
     /*Example
     public void Example(){
         SetCanvasState (CanvasStates.PopUp);
     }
     */
     public void SetCanvasState(CanvasStates state){
         hud.SetActive (state == CanvasStates.Hud);
         popUpMenu.SetActive (state == CanvasStates.PopUp);
         settingsMenu.SetActive (state == CanvasStates.Setting);
     }
 
 }
 
 


Please let me know if you face any difficulty regarding this. I havn't tested the script.

Comment
Add comment · Share
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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object reference not set to an instance of an object... 0 Answers

Variables are assigned but it returns null 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

NullRefenceException error 1 Answer


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