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.
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
}
}
}
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".
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
![](https://koobas.hobune.stream/wayback/20220612084630im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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