When the time exceeds the max, it spams me with NullReferenceException errors. I can't seem to figure out why it would give me this error. The errors happen on the same lines of code, both of which being the nested if
statements. It's probably the scene.score
but score
is defined within the PlayerScript
script.
public int timeLimit = 15;
public float startTime = 0;
PlayerScript scene = GetComponent<PlayerScript>();
if (startTime >= timeLimit)
{
if (scene.score >= 25)
{
SceneManager.LoadScene("WinScene");
}
if (scene.score < 25)
{
SceneManager.LoadScene("LoseScene");
}
}
EDIT
when I test it in Unity, after the time exceeds 15 seconds, it gives me errors telling me that it is on a certain line of code. That being the first nested if statement. I commented out that first one and left the second one to test, and sure enough it told me that this new line was causing the error.
EDIT
Looks like switching GetComponent
with FindObjectOfType
did the trick. <PlayerScript>
is a different script that I was trying to pull info from, ie. score
.