0

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.

Airn5475
  • 2,116
  • 25
  • 43
Shainyyy
  • 25
  • 3
  • Primitive types like `int`, `string` etc are value types. The assignment you make in `Start` is a one-time "copy" of the value .. if it changes afterwards it doesn't get updated. – derHugo Aug 27 '19 at 17:47
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) -> is `Player` referenced via the Inspector? Does it have the `PlayerController` component? – derHugo Aug 27 '19 at 17:48

2 Answers2

0

You're setting the counter variable at Start(), but it doesn't get updated when the variable is updated in the other script. This happens because even though you set it to the value of jumpCounter on player at Start(), it isn't "bound" to that value. You are essentially just creating another variable with the value of jumpCounter. Try the following instead:

private void OnCollisionEnter2D(Collision2D other)
{
    if (other.gameObject.name == "Hand")
    {
        counter = Player.GetComponent<PlayerController>().jumpCounter;
        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();

        }
    }
}

Here, instead of setting it at Start(), you're setting the variable when the collision happens.

Brandon Miller
  • 1,334
  • 1
  • 7
  • 12
  • If i do this, the collison doesnt work, so it "blocks" to set the panel to true and freeze the player.. error code: NullReferenceException: Object reference not set to an instance of an object GameScript.OnCollisionEnter2D (UnityEngine.Collision2D other) (at Assets/Scripts/GameScript.cs:33) just the same. – Shainyyy Aug 27 '19 at 17:54
  • Alright, I now whats the mistake. I said: Player.Getcomponent... But its not the player, i have a playercontroller script on a othe gameobject... But now its solved thank you – Shainyyy Aug 27 '19 at 17:58
  • Glad you found the issue! :) – Brandon Miller Aug 27 '19 at 17:59
0

I think it is more efficient to cache the reference to your script and get the value during collide. Calling GetComponent<> is always expensive. So:

PlayerController playercontroller; 

define it on Start():

playercontroller = Player.GetComponent<PlayerController>();

and use it in your OnCollisionEnter2D as:

playercontroller.jumpCounter
Aykut Karaca
  • 348
  • 2
  • 15