0
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ If you want to ignore bird collision with other players, the easier way is to use the physics layer docs.unity3d.com/Manual/LayerBasedCollision.html \$\endgroup\$
    – Zibelas
    Commented Jan 18, 2022 at 9:56
  • \$\begingroup\$ @Zibelas thank u so much, this helped . Initially it didnt work because it was under Physics I unchecked, but later when I did it under Physics 2D it work...Cheeers mate \$\endgroup\$ Commented Jan 18, 2022 at 12:13

3 Answers 3

1
\$\begingroup\$

It appears like your "playerBird" game object has no component of type Collider2D. Make sure that one of the 2D colliders is attached. When you want to find it via GetComponent, then it must be attached to the object directly. When the collider is attached to a sub-object of the "playerBird", use GetComponentInChildren instead.

Also keep in mind that there are also colliders which do not have the suffix "2D". These are 3D colliders used by the 3D physics engine. 2D and 3D colliders are completely separate systems and do not interact with each other.

\$\endgroup\$
0
\$\begingroup\$

While this is not fixing your nullpointers, it will have the same result in the end. You are trying to solve a collision exception manually. Good for you that Unity ships this with a simple collision matrix. If you are using 2D physics, you need to use the 2D version.

https://docs.unity3d.com/Manual/LayerBasedCollision.html

\$\endgroup\$
0
\$\begingroup\$

Your playerBird is not null, but apparently does not have Collider2D attached, hence this call:

playerBird.GetComponent<Collider2D>() returns null

\$\endgroup\$
2
  • \$\begingroup\$ This has been mentioned in another answer; why do you feel the need to repeat it? \$\endgroup\$
    – Vaillancourt
    Commented Feb 18, 2022 at 10:37
  • \$\begingroup\$ @Vaillancourt I added information that entire function call to GetComponent returns null which must be checked indepedently of playerBird which may be not null \$\endgroup\$
    – PiotrK
    Commented Feb 21, 2022 at 14:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .