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?