Search Unity

Why am I getting Null Reference Exception inspite of checking if the Object is not null to execute s

Discussion in 'Scripting' started by Chethan007, Jan 18, 2022.

  1. Chethan007

    Chethan007

    Joined:
    Dec 3, 2020
    Posts:
    56
    Hi I am just trying to convert a simple flappy bird game into multiplayer game.

    So In the multiplayer game using Photon , I want the bird to collide with the ground or pipe but not with the enemy bird .i.e., ignore the enemy's bird collision. I somehow worked it out,code below.

    So I have tagged the bird under a name called "BirdClass" , and in the start I have the following code to ignore the coillision for the first time:

    void Start()
    {
    PV = GetComponent<PhotonView>();
    playerBird = GameObject.FindGameObjectWithTag("BirdClass");
    if (playerBird != null)
    Physics2D.IgnoreCollision(playerBird.GetComponent<Collider2D>(), GetComponent<Collider2D>());

    }

    Over here I get the following error:

    ArgumentNullException: Value cannot be null. Parameter name: collider1 UnityEngine.Physics2D.IgnoreCollision (UnityEngine.Collider2D collider1, UnityEngine.Collider2D collider2) (at <4e5075cf57c3416eb788a8bd41817a84>:0) BirdPigeon.Start () (at Assets/Scripts/BirdPigeon.cs:45)

    and the onCollider2d function looks like this:

    private void OnCollisionEnter2D(Collision2D collision)
    {
    playerBird = GameObject.FindGameObjectWithTag("BirdClass");
    if (isMultiPlayerGame && playerBird != null && PV.IsMine)
    {
    if (collision.gameObject.tag == playerBird.gameObject.tag)
    {
    Physics2D.IgnoreCollision(collision.gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    }
    else
    {
    BirdCollided?.Invoke(true);
    }
    }
    if (!isMultiPlayerGame)
    {
    BirdCollided?.Invoke(true);
    rigidBodyOfBird.velocity = Vector2.zero;
    }
    }

    and here I get the error like:

    "NullReferenceException: Object reference not set to an instance of an object
    BirdPigeon.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/BirdPigeon.cs:81)"

    I can't understand, though I am checking whether it is null or not ,why I am still getting these errors?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,458
    Use code-tags when posting code, plain text is crazy difficult to follow and we don't get line numbers either so your post that says line 81 means we'd have to count 81 lines down to find it. You can edit your post above for this.

    This is the single most common error posted on these forums. There must be dozens a day. You have a reference to a class which is NULL and you're using it. It tells you the line, you only have to figure out what is NULL. You say you check for NULL, that's fine but you then use "GetComponent" which can also return NULL itself if that component couldn't be found and there's simply no benefit from assume it does have that component if it's returning NULL. Stuff like this.

    ArgumentNullException: Value cannot be null. Parameter name: collider1 UnityEngine.Physics2D.IgnoreCollision (UnityEngine.Collider2D collider1, UnityEngine.Collider2D collider2) (at <4e5075cf57c3416eb788a8bd41817a84>:0) BirdPigeon.Start () (at Assets/Scripts/BirdPigeon.cs:45)

    Line 45 of BirdPigeon. Calling Physics2D.IgnoreCollision. The Argument named "collider1" is being passed NULL. I mean, there's simply no need to throw your hands in the air as it's telling you everything that is wrong so I just don't understand what's the confusion here if I'm honest. :)
     
  3. Chethan007

    Chethan007

    Joined:
    Dec 3, 2020
    Posts:
    56
    Oh Sorry about that, was really tired when I posted this. I tried using the layers in Unity and it worked. Defintely thanks for the answer
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,694
    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

    - drag it in using the inspector
    - code inside this script initializes it
    - some OTHER external code initializes it
    - ? something else?

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032