This is my first game and I’m trying to reset the game after player death, i already made the ui and all the functions but for some reason after the player dies and reloads the scene the enemy manager stops respawning my enemy prefab after I kill it once. The error message appears when I try to instantiate the new enemy. The error message is. “MissingReferenceException: The object of type ‘EnemyManager’ has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. EnemyManager.spawnNewEnemy () (at Assets/EnemyManager.cs:35) target.g__Die|26_0 () (at Assets/target.cs:116) target.TakeDamage (System.Single amount) (at Assets/target.cs:103) gun.Shoot () (at Assets/gun.cs:59) gun.Update () (at Assets/gun.cs:34)”
target.cs
public class target : MonoBehaviour
{
public float health = 30f;
public NavMeshAgent agent;
public Transform player;
public LayerMask whatIsGround, whatIsPlayer;
public HealthbarEnemy healthbar;
public delegate void EnemyKilled();
public static event EnemyKilled OnEnemyKilled;
//Patroling
public Vector3 walkPoint;
bool walkPointset;
public float walkPointRange;
//Attacking
public float timeBetweenAttacks;
public float agentDamage = 50f;
bool alreadyAttacked;
//State
public float sightRange, attackRange;
public bool playerInSightRange, PlayerInAttackRange;
private void Awake()
{
player = GameObject.Find("First Person Player").transform;
agent = GetComponent<NavMeshAgent>();
}
public void Patroling()
{
if (!walkPointset)
{
SearchWalkPoint();
}
if (walkPointset)
{
agent.SetDestination(walkPoint);
}
Vector3 distaneTowalkPoint = transform.position - walkPoint;
if (distaneTowalkPoint.magnitude < 1f)
{
walkPointset = false;
}
}
private void SearchWalkPoint()
{
float randomZ = Random.Range(-walkPointRange, walkPointRange);
float randomX = Random.Range(-walkPointRange, walkPointRange);
walkPoint = new Vector3(transform.position.x + randomX, transform.position.y,
transform.position.z + randomZ);
if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
{
walkPointset = true;
}
}
public void ChasePlayer()
{
agent.SetDestination(player.position);
}
public void AttackPlayer()
{
agent.SetDestination(transform.position);
transform.LookAt(player);
if (!alreadyAttacked)
{
//attack code here
player.GetComponent<playerDamage>().damagePlayer(agentDamage);
alreadyAttacked = true;
Invoke(nameof(ResetAttack), timeBetweenAttacks);
}
}
private void ResetAttack()
{
alreadyAttacked = false;
}
public void TakeDamage (float amount)
{
health -= amount;
healthbar.SetHealth(health);
if(health <= 0f)
{
Die();
}
void Die()
{
Debug.Log("this needs to be excuted");
if (gameObject != null)
{
Debug.Log("this needs to be excuted");
Destroy(gameObject);
Debug.Log("this was excuted successfully");
}
Debug.Log("this needs to be excuted");
OnEnemyKilled();
Debug.Log("this was excuted successfully");
}
}
private void Update()
{
player = GameObject.Find("First Person Player").transform;
agent = GetComponent<NavMeshAgent>();
//Check for sight and attack range
playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
PlayerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
if(!playerInSightRange && !PlayerInAttackRange)
{
Patroling();
}
if (health < 30f)
{
ChasePlayer();
}
if (playerInSightRange && !PlayerInAttackRange)
{
ChasePlayer();
}
if (playerInSightRange && PlayerInAttackRange)
{
AttackPlayer();
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, attackRange);
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, sightRange);
}
}
gun.cs
public class gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 3000f;
public AudioSource soundSource;
public AudioClip gunschot;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffectBlood;
public GameObject impactEffectGround;
public pausmenu pausmenu;
private float nextTimeToFire = 0f;
void Start()
{
soundSource = GetComponent<AudioSource>();
impactEffectBlood.SetActive(true);
impactEffectGround.SetActive(true);
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire && !pausmenu.GameIsPaused)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
void playgunsound ()
{
soundSource.clip = gunschot;
soundSource.PlayOneShot(soundSource.clip);
}
void Shoot()
{
playgunsound();
muzzleFlash.Play();
RaycastHit hit;
if(Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
target target = hit.transform.GetComponent<target>();
if(target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
if (hit.transform.name == "Enemy")
{
GameObject impactGO = Instantiate(impactEffectBlood, hit.point,
Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
if (hit.transform.tag == "ground")
{
GameObject impactGO = Instantiate(impactEffectGround, hit.point,
Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
}
}
EnemyManager.cs
public class EnemyManager : MonoBehaviour
{
public Transform spawnpoint;
public GameObject EnemyPrefab;
// Start is called before the first frame update
void Start()
{
Debug.Log("this needs to be excuted");
spawnNewEnemy();
Debug.Log("this was excuted successfully");
}
void OnEnable()
{
Debug.Log("this needs to be excuted");
target.OnEnemyKilled += spawnNewEnemy;
Debug.Log("this was excuted successfully");
}
// Update is called once per frame
void Update()
{
}
void spawnNewEnemy()
{
Debug.Log("this needs to be excuted");
if (transform != null)
{
Debug.Log("this needs to be excuted");
Debug.Log(transform);
Instantiate(EnemyPrefab, spawnpoint.transform.position, Quaternion.Euler(Vector3.zero));
Debug.Log("this was excuted successfully");
} else
{
Debug.Log("ENEMYMANAGER.TRANSFORM = NULL");
}
}
}