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.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
//public Text level = SceneManager.GetActiveScene().buildIndex.ToString("0");
public class levelCounter : MonoBehaviour {
//public int levelCount;
public Text levelText;
private void Start()
{
//levelCount = SceneManager.GetActiveScene().buildIndex;
}
// Update is called once per frame
public void levelUpdate ()
{
//levelCount++;
Debug.Log("Triggered");
int buildIndex = SceneManager.GetActiveScene().buildIndex;
levelText.text = buildIndex.ToString("0");
Debug.Log(SceneManager.GetActiveScene().buildIndex);
}
}
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:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
//public Text level = SceneManager.GetActiveScene().buildIndex.ToString("0");
public class levelCounter : MonoBehaviour {
public Text levelText;
public int buildIndex;
private void Start()
{
buildIndex = SceneManager.GetActiveScene().buildIndex;
Debug.Log(SceneManager.GetActiveScene().buildIndex + 1);
levelText.text = "Level: " + buildIndex.ToString("0");
}
public void levelUpdate ()
{
Debug.Log("Triggered");
levelText.text = "Level: " + buildIndex.ToString("0");
Debug.Log(SceneManager.GetActiveScene().buildIndex+1);
}
}
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?