My dungeon game thing, has an objective hidden inside, once the player collects the objective the scene restarts and a new dungeon is created. I have a level counter text object in my game that shows the player how many levels he has completed, currently the score reverts to 0 after the scene reloads but I want it to increment every time the player collects the objective.
This file is on the Character object which is a child of a FPSController object, The other irrelevant code is just checking to see if the player has fallen through a hole and died.
public static int levelCounter;
public GameObject text;
// Start is called before the first frame update
void Awake()
{
levelCounter = text.GetComponent<LevelText>().level;
}
void Start()
{
}
// Update is called once per frame
void Update()
{
if (gameObject.transform.position.y < -1)
{
SceneManager.LoadSceneAsync("Death");
levelCounter = 0;
}
}
This is the code that is run if the player collects the objective, the level counter is incremented and the scene reloads
private AudioSource pickupSoundSource;
void Awake() {
pickupSoundSource = DontDestroy.instance.GetComponents<AudioSource>()[1];
}
void OnControllerColliderHit(ControllerColliderHit hit) {
if (hit.gameObject.tag == "Pickup") {
pickupSoundSource.Play();
CharacterScript.levelCounter += 1;
SceneManager.LoadScene("Play");
}
}
This is for the text object that actually displays the level counter during the game
public GameObject player;
private Text text;
public int level;
// Start is called before the first frame update
void Start()
{
level = 0;
text = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
level = CharacterScript.levelCounter;
text.text = "Level: " + level;
}
I've tried fiddling around with static variables and DoNotDestroyOnLoad() but nothing seems to work.