I just started making my game and it was working fine, the damage system was working with the attack button but i was doing some other coding not even effecting the button script or the scripts it was referencing and it started giving me this error.
NullReferenceException: Object reference not set to an instance of an object
PlayerAttack.PlayerAttackEnemy () (at Assets/Scripts/PlayerAttack.cs:14)
PlayerAttacks Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack : MonoBehaviour
{
public GameObject Player;
public void PlayerAttackEnemy()
{
Player = GameObject.Find("Player");
PlayerManager PlayerManager = Player.GetComponent<PlayerManager>();
PlayerManager.playerAttack = true;
}
}
}
And the PlayerManager Script:
public class PlayerManager : MonoBehaviour
{
//Variables
public int maxHealthP = 15;
public int curHealthP = 15;
public int playeLevel = 1;
public int damageP = 1;
public bool playerAttack;
public GameObject Enemy;
private void Start()
{
Enemy = GameObject.Find("Enemy");
playerAttack = false;
}
//Take Damage
public void TakeDamagePlayer(int damage)
{
curHealthP -= damage;
Debug.Log(curHealthP);
}
//Do Damage
void DoDamagePlayer(int damage)
{
EnemyManager EnemyManager = Enemy.GetComponent<EnemyManager>();
EnemyManager.TakeDamageEnemy(damage);
}
//Attack
void Attack(int damage)
{
DoDamagePlayer(damage);
playerAttack = false;
}
//Die
void Die()
{
Object.Destroy(this);
}
void FixedUpdate()
{
if (playerAttack == true)
{
Attack(damageP);
}
if (curHealthP >= 0)
{
Die();
}
}
}
Thanks in advance