Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Need help implementing level counter

Discussion in 'Scripting' started by jojomonster679, Oct 19, 2018.

  1. jojomonster679

    jojomonster679

    Joined:
    Oct 19, 2018
    Posts:
    14
    I am extremely new to Unity and would love some help getting a level counter put into my game. Basically, I am only trying to display the level the player is on in the game. This is what I have written so far, and though the debug.logs are triggering, the text on the game is not changing (and yes I have assigned the text variable in the inspector). UPDATE: the levelUpdate method is triggering every time the scene changes.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.SceneManagement;
    4.  
    5. //public Text level = SceneManager.GetActiveScene().buildIndex.ToString("0");
    6.  
    7.  
    8. public class levelCounter : MonoBehaviour {
    9.  
    10.     //public int levelCount;
    11.     public Text levelText;
    12.  
    13.     private void Start()
    14.     {
    15.         //levelCount = SceneManager.GetActiveScene().buildIndex;
    16.     }
    17.     // Update is called once per frame
    18.     public void levelUpdate ()
    19.     {
    20.         //levelCount++;
    21.         Debug.Log("Triggered");
    22.         int buildIndex = SceneManager.GetActiveScene().buildIndex;
    23.         levelText.text = buildIndex.ToString("0");
    24.         Debug.Log(SceneManager.GetActiveScene().buildIndex);
    25.     }
    26. }
    Thanks for any help you are able to provide!
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't see how the following would result in anything but a string of "0", though I'm rusty on my custom string formatting.

    Code (csharp):
    1. levelText.text = buildIndex.ToString("0");
    Just try without the custom formatting first, and work from there.

    Code (csharp):
    1. levelText.text = buildIndex.ToString();
     
  3. jojomonster679

    jojomonster679

    Joined:
    Oct 19, 2018
    Posts:
    14
    Thanks for the quick reply! I figured it out in the past twenty minutes, I declared the integer buildIndex in the start function, and after that, it just started to work! Full working code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.SceneManagement;
    4.  
    5. //public Text level = SceneManager.GetActiveScene().buildIndex.ToString("0");
    6.  
    7.  
    8. public class levelCounter : MonoBehaviour {
    9.  
    10.     public Text levelText;
    11.     public int buildIndex;
    12.  
    13.  
    14.     private void Start()
    15.     {
    16.         buildIndex = SceneManager.GetActiveScene().buildIndex;
    17.         Debug.Log(SceneManager.GetActiveScene().buildIndex + 1);
    18.         levelText.text = "Level: " + buildIndex.ToString("0");
    19.     }
    20.    
    21.     public void levelUpdate ()
    22.     {
    23.         Debug.Log("Triggered");
    24.         levelText.text = "Level: " + buildIndex.ToString("0");
    25.         Debug.Log(SceneManager.GetActiveScene().buildIndex+1);
    26.     }
    27. }
    28.  
     
  4. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    It sounds like your going to use this variable through out your game. Just a suggestion, have you considered storing it in Player Prefs or something global so you can access it across the scenes?
     
  5. jojomonster679

    jojomonster679

    Joined:
    Oct 19, 2018
    Posts:
    14
    I attached it to an object that is a child of my UI canvas that is a prefab used throughout the game. Is that the same thing?