I have a TextObject in my script which is attached to my canvas, as seen in the screenshots. Whenevery i try to run it i get that error, I tried many things but can't seem to figure out why this error appears.
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
//TODO: Implement Death screen when health falls to 0 - respawn priority(high)
public class DeathScreen:MonoBehaviour
{
public Text timeLived;
public Button replay;
public Button mainMenu;
public Button retry;
// ReSharper disable Unity.PerformanceAnalysis
public void Setup(int timeLivedInSeconds)
{
//SceneManager.UnloadSceneAsync("scenes/main");
SceneManager.LoadScene("Scenes/deathScene");
timeLived.text = "Time Survived: " + timeLivedInSeconds;
retry.onClick.AddListener(Retry);
replay.onClick.AddListener(Replay);
mainMenu.onClick.AddListener(MainMenu);
}
public void Replay()
{
//TODO: call Replay
}
public void Retry()
{
SceneManager.UnloadSceneAsync("scenes/death");
SceneManager.LoadScene("scenes/main");
}
public void MainMenu()
{
SceneManager.UnloadSceneAsync("scenes/death");
SceneManager.LoadScene("scenes/menu");
}
}
ERROR:
NullReferenceException: Object reference not set to an instance of an object
DeathScreen.Setup (System.Int32 timeLivedInSeconds) (at Assets/Scripts/Mechanics/DeathScreen.cs:23)
DeathScreen.Start () (at Assets/Scripts/Mechanics/DeathScreen.cs:16)