I am making a simple 2D game and I made a projectile that flies towards the player and calls a method from the player script when it touches it.
The strangest thing about this error is that it worked before. A few hours ago it has worked perfectly fine and all of a sudden it is throwing this exception at me, but I didn’t even touch the script attached to my projectile.
projectile script:
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
public Player player;
void Start ()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("Player"))
{
player.Damage();
}
}
}
The method I am calling from my Player script:
public void Damage()
{
tookDamage = true;
anim.SetBool("PlayerHit", true);
HP -= 1;
}
The error:
NullReferenceException: Object reference not set to an instance of an object
Bullet.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/Bullet.cs:17)