Hi im having a problem with this i tried for 2 Days now but i don't know how to fix it. Im a beginner in C# and hope you can help me. Im trying to use a variable from another script but the console keeps sending me an error: NullReferenceException: Object reference not set to an instance of an object
PlayerCombat.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scrips/PlayerCombat.cs:52)
public int attackDmg = 2; //Diffrent Script
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "Enemy")
{
PlayerTakeDamage(currentPlayerHp, GetComponent<Enemy>().attackDmg);
}
}
static void PlayerTakeDamage(int currentPlayerHp, int attackDmg)
{
currentPlayerHp -= attackDmg;
if (currentPlayerHp <= 0)
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}