-1

Hi everyone so I´m having this error on my console and I don´t really now what it means I´m new to this so that´s the reason I´m here. So I have some ladders to climb and I have a roll ability and when I roll and enter a ladder this pops up.

NullReferenceException: Object reference not set to an instance of an object Ladders.OnTriggerStay2D (UnityEngine.Collider2D collision) (at Assets/Scripts/Interactables/Ladders.cs:26)

so now here´s my ladders script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ladders : MonoBehaviour
{
    private GatherInput gI;
    private PlayerMoveControls pMC;
    //private Shoot shoot;

    void Start()
    {
          
    }
    
    private void OnTriggerEnter2D(Collider2D collision)
    {
        gI = collision.GetComponent<GatherInput>();
        pMC = collision.GetComponent<PlayerMoveControls>();
        
        //shoot = collision.GetComponent<Shoot>();
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
        if(gI.tryToClimb)
        {
            pMC.onLadders = true;
            //shoot.enabled = false;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        pMC.ExitLadders();
        //shoot.enabled = true;
    }
}

can anyone help me on how to solve this ?? now I have to mention that everything is working fine while playing the game all the abilities and animations everything is ok but I do get that error. thank you very much

  • Have you tried using the debugger? – PMF Dec 10 '21 at 11:02
  • Sounds like either `gI` or `pMC` wasn't found on the colliding object ... btw in general for your own happiness sake: Use better field names! Later your code is compiled into a minimal machine code anyway so as long as you deal with `c#` use some meaningful names like `GatherInput gatherInput;` and `PlayerMoveControls playerMoveControls;` ! – derHugo Dec 10 '21 at 11:14
  • ok I will try to start using some better names ;) thank you for the advice – David Spinola Dec 10 '21 at 11:25

1 Answers1

0

A NullReferenceException is when you try to reference a variable that is currently null. That means something in your code is not set. The error says it is located at Assets/Scripts/Interactables/Ladders.cs:26. That is line 26.

On that line you have if(gI.tryToClimb). I am willing to bet the value of gI is null at some point in time. So when you try to access .tryToClimb it will give an error. You will want to prevent this error by checking if gI is null first. It's always a good idea to check if something is null before using it. Try this.

private void OnTriggerStay2D(Collider2D collision)
{
    if(gI != null && gI.tryToClimb)
    {
        if(pMC != null)
        {
           pMC.onLadders = true;
           //shoot.enabled = false;
        }
    }
}

You may also want to check for null here as well:

private void OnTriggerExit2D(Collider2D collision)
{
    if(pMC != null)
    {
       pMC.ExitLadders();
       //shoot.enabled = true;
    }
}
DSander
  • 1,094
  • 6
  • 9
  • This does hide the exception yes ... but exceptions have a purpose! Rather find out what is actually wrong and fix it! Also in particular for Unity you should **not** use `!= null` but **if** you check for existence rather use `if(pMC){ ... }` – derHugo Dec 10 '21 at 11:12
  • Hey DSander how are you ? man thank you so much for your help I went to the script and changed it to what you post and it resolve the issue so thank you very much now it would be nice to understand where the script is getting null but yeah you fix it !! thank you very much – David Spinola Dec 10 '21 at 11:17
  • 1
    @derHugo Unless they share the entire game project that would be difficult to do. This isn't hiding an error, this is handling an edge case. OnTriggerEnter they are setting gI. Then OnTriggerStay gI should already be set. The issue can arise if something else collides with and OnTriggerEnter and they don't have the GatherInput component, so it sets gI to null. I don't feel getting this in depth is necessary. This member is obviously new and had a simple question, and I gave a simple answer. – DSander Dec 10 '21 at 11:18
  • I will go and try to see where the script gets null now that gI or gather input is the script that my player has and it´s the only game object that has that script – David Spinola Dec 10 '21 at 11:20
  • I'm glad I can help David. Please accept my answer if you get a chance. – DSander Dec 10 '21 at 11:27
  • 1
    @derHugo you can only use if(pMC) if the object is derived from UnityEngine.Object or another class that implicitly handles that compare operator. Otherwise if(pMC != null) should be used. If you want to be new age you can also use if(pMB is null) Either way they both have the same outcome when used. For beginners I like to show them the explicit way, because otherwise I would have to explain the inner workings of if() treating an object like a boolean when its not one. :) – DSander Dec 10 '21 at 11:44
  • @DSander OP uses `GetComponent` so we know that it has to derive from `UnityEngin.Object` though ;) And I think explaining this is not that much out of scope since you want to check for existence here. What I mean by it "hides" the exception is: How do you know if OP collides with another object or just forgot to add according component to the player object(s)? => The solution is rather: @OP debug your code and find out **why** it is `null` at this point. E.g. if this is from another object, why not make sure via Layers that these objects can not collide at all in the first place? – derHugo Dec 10 '21 at 11:55
  • @derHugo you should know that `GetComponent` also accepts types that are `Interface` which will not work with `if(pMC)` by default. It is best to suggest an answer that will work in call cases, not just specific cases. Regarding scope, the OP clearly said "everything is working fine while playing the game". That indicates the issue is an edge case, and its safe to prevent said case until it needs to be officially handled. We can assume the OP will dig deeper on their own time. So, fix the issue at hand and move on. They should ALWAYS check for NULL so its not like this is some quick fix. – DSander Dec 10 '21 at 23:26