- Home /
Force reset static variable
After googling a bit, I've tried some methods to try to reset a static variable, but none of them want to work (Sure I'm doing some wrong steps). First I tried to Reset the value to 0, both via Start, and Update, like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameVariables : MonoBehaviour {
public static int keyCount;
public static int resetkeyCount = 0;
void Start () {
keyCount = resetKeyCount;
}
void Update () {
if (Input.GetKeyDown(KeyCode.Escape)) {
Reset();
}
}
void Reset (){
keyCount = resetKeyCount;
}
}
And after by trying to instantiate the Class, like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameVariables : MonoBehaviour {
public static GameVariables Instance;
public int keyCount;
void Start () {
Instance = this;
}
}
And accesing the keyCount via "GameVariables.Instance.keyCount"; but Unity says "Object reference not set to an instance of an object." Which doesn't help to achieve the goal.
Since the objective for the player is collecting "X" keys, the goal with the code is to reset the keyCount each time the scene is loaded.
If anyone has an idea of how to proceed, it'll be great.
Thanks in advance.
Answer by chillersanim · Nov 29, 2016 at 02:32 PM
It looks like you are trying to implement a singleton behaviour.
If so, this code of yours is not complete. The problem you face, is that the Instance variable may not be set when you access it.
I assume you access it from outside the class?
Possible solution:
A way around would be, to create a public static property, with only a get-accessor.
When the property is accessed, it should check whether an instance exists, if not, it should create one.
Only then it should give that instance back.
Taking a step back:
Your class is called GameVariables, this sounds like a storage only class.
So why make it a MonoBehaviour at all? This restricts your use alot.
If you don't need the class to inherit from MonoBehaviour, I would sugest to just create a static class, inheriting from object.
Example:
public class GameVariables
{
private static GameVariables instance;
public static GameVariables Instance
{
get
{
// Returns instance. Or if null, creates new instance, stores it and returns it.
return instance ?? (instance = new GameVariables());
}
}
// Private in order to prevent multiple instances
private GameVariables()
{
// Initialize instance here
}
}
Be aware, this implementation is NOT perfect.
It works in most cases, but when using multithreading, it is not complete.
For more information, see this link: https://msdn.microsoft.com/en-us/library/ff650316.aspx
Edit:
If you realy need a MonoBehaviour singleton, the code would look a little different:
public class GameVariables : MonoBehaviour
{
private static GameVariables instance;
public static GameVariables Instance
{
get
{
if (instance == null)
{
// Create new GO and add component
var go = new GameObject();
instance = go.AddComponent<GameVariables>();
}
return instance;
}
}
private void OnEnable()
{
// Prevent game developer from instantiating multiple singletons...
if (instance != null && instance != this)
{
Destroy(this);
return;
}
instance = this;
}
private void OnDestroy()
{
// Only empty the instance when it was this
if (instance == this)
{
instance = null;
}
}
}
Here again, the code is NOT perfect.
If you use multithreading or access the singleton when the application is quitting, it could cause errors.
For more information, see this link: http://wiki.unity3d.com/index.php/Singleton
Greetings
Chillersanim
Your implementation wouldn't work. A static class can't be instantiated and can only contain static members (static methods, fields, properties). If you want to implement a singleton, the class must not be static.
Whops, you are right.
I've written the code from the top of my head, but that's a stupid one...
Anyways, I've fixed it. Thanks :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612101641im_/https://answers.unity.com/themes/thub/images/avi.jpg)