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.
↧