I try to program a little game. My Problem: I have a jumpcounter, which does what the name is. I want to display this amount of jumps in a Panel, which appears if I collide with the "goal". But if I collide, and the panel appears there is only: Jumps needed: 0 And I get the following error code:
NullReferenceException: Object reference not set to an instance of an object GameScript.Start () (at Assets/Scripts/GameScript.cs:28)
The biggest problem is, that the variable is out of another script, the jumpcounter on its own works fine
I did what a lot of videos on YouTube say, so checking that I really attached the UI Text to my public Text, but I saw no video with a really useful answer...
private void Start()
{
counter = Player.GetComponent<PlayerController>().jumpCounter;
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.name == "Hand")
{
LevelFinishedPanel.SetActive(true);
Player.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;
if (counter <= maxJumpsForGold)
{
ShowUpJumpCounter.text = "Jumps you needed: " + counter.ToString();
Gold.enabled = true;
}
else if (counter <= maxJumpsForSilver && counter > maxJumpsForGold)
{
ShowUpJumpCounter.text = "Jumps you needed: " + counter.ToString();
Silver.enabled = true;
}
else if (counter <= maxJumpsForBronze && counter > maxJumpsForSilver)
{
ShowUpJumpCounter.text = "Jumps you needed: " + counter.ToString();
Bronze.enabled = true;
}
else
{
ShowUpJumpCounter.text = "Jumps you needed: " + counter.ToString();
}
I expect, that the amount of my jumps is displayed, but actually its just 0.