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 /
  • Help Room /
avatar image
0
Question by jamesstephenbrown · Jul 11, 2016 at 06:07 PM · c#nullreferenceexceptionclasses

How to store variables in a separate class

I'm new to Unity and have been coding in Swift for a while. Usually when creating game in Swift I would have a utility class that would exist outside of the game scene. I am trying to implement this in Unity C# and am having all sorts of trouble.

Here is my (simplified) Load script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class Load : MonoBehaviour {

 public GameObject player;
 public GameObject goal;
 public Util util;

 void Start () {
     
     Instantiate(player, new Vector2(0, -4), Quaternion.identity);
     Instantiate(goal, new Vector2(0, 4), Quaternion.identity);

     if (util.level == 0) { // this is line 18
             // do stuff
     }

     }

Here is my (simplified) Utility class:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class Util {

     public int level = 0;

 }

I am getting a NullReferenceException: Object reference not set to an instance of an object Load.Start () (at Assets/Scripts/Load.cs:18)

This is the first time I'm trying to access the utility class. I am thinking that I might need to attach it to a GameObject and call GetComponent each time I want to access it. But this seems very clumsy, is the a way to access the variables using dot syntax? Or is this approach contrary to the way Unity works?

Thanks, James.

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

1 Reply

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

Answer by jdean300 · Jul 11, 2016 at 06:12 PM

It seems like you only want one Util class to exist at any point and to be able to access it anywhere. That's a static class:

 public static class Util
 {
      public static int level = 0;
 }

With this your load class would become:

 public class Load : MonoBehaviour
 {
     public GameObject player;
     public GameObject goal;
 
     void Start()
     {
         Instantiate(/*yada yada*/); 
         Instantiate(/*yada yada*/); 
         if (Util.level == 0){
             //do stuff
         }
     }
 }
Comment
Add comment · Show 2 · 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 jamesstephenbrown · Jul 11, 2016 at 06:26 PM 0
Share

Thank you!

I think that's exactly what I was after, and so clean! However, being static, does that mean I can't change the variables? Obviously, I'm going to want to increment the level, I assume this is still possible? I'm probably misinterpreting the word "static".

avatar image jdean300 jamesstephenbrown · Jul 11, 2016 at 06:31 PM 0
Share

Yes you can still change the variables. Static simply means that you do not need a reference to the class in order to use the variables, so you don't have to write new Util() to access the variable. In fact, because the class is static, you cannot instantiate the class with new Util().

You can have static variables inside of a non-static class:

 public class $$anonymous$$yClass
 {
     public static int StaticVar;
 }

And then you can access and edit the variable in two ways:

 //Just using the name of the class
 $$anonymous$$yClass.StaticVar = 5;
 
 //The class is not static, so we can instantiate it
 $$anonymous$$yClass m = new $$anonymous$$yClass();
 //m can access the variable as if it was it's own variable
 m.StaticVar = 6;
 
 $$anonymous$$yClass m2 = new $$anonymous$$yClass();
 //The static var is shared between all instances of the class, so this will log 6
 Debug.Log(m2.StaticVar);

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

183 People are following this question.

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

Related Questions

Object reference not set to an instance of an object - Jumping Scripts (C#) 1 Answer

NullReferenceException: Object reference not set to an instance of an object in Singleton class. 1 Answer

Getting "object reference not set" error when creating a list dictionary. 1 Answer

Class extension (player.STR to player.Stats.STR) 1 Answer

Instantiation Problem 0 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