1

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.

TKeys23
  • 23
  • 1
  • 4

1 Answers1

-1

There are many ways to achieve this, depends on what you want.

1- If you want to hold the variable value when you reload but not when you restart the game, you can follow the singletone pattern where you dont destroy the gameobject that have the script which holding the level counter on reload by calling DontDestroyOnLoad at awake funtion, but you have to make sure you dont have many instance of the same class when the scene reload

2- Use unity PlayerPref to store values and load them when you restart the level

You can check the duplicate question in the top

VectorX
  • 657
  • 5
  • 12
  • There are a lot more ways ;) Please see the duplicate link for detailed answers and a lot more (and better) solutions – derHugo Jul 21 '20 at 06:17
  • @derHugo thanks, I just updated the answer, I just answered the question on fly, but that should not be considered as wrong answer! – VectorX Jul 21 '20 at 07:45
  • No I wouldn't say wrong .. just really not complete and if you compare it to the linked question and answers there I would say yours can't really hold up with the quality ;) – derHugo Jul 21 '20 at 15:04