Missing Reference Exception

So when my enemy game object get destroy by the player, an error pop up
“MissingReferenceException: The object of type ‘Transform’ 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.”
I’m not sure how to fix this, if anyone can help me that would be great
Here my Script:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;

public class PlayerAIBehaviour : MonoBehaviour
{
    public float playermaxhealth; //maxhealth
    public float playercurrenthealth; //currenthealth
    public float damage; //damage
    public float speed; //speed
    public float attackrange; //attackrange
  
    public bool moving;
    private Transform enemy;
    Rigidbody2D rb;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
        moving = true;
        playercurrenthealth = playermaxhealth;
    }

    // Update is called once per frame
    void Update()
    {
        if (moving == true)
        {
            transform.position += Vector3.right * speed * Time.deltaTime;
        }
        float distanceFromEnemy = Vector2.Distance(enemy.position, transform.position);
        if (distanceFromEnemy < attackrange)
        {
            moving = false;
        }
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Enemy")
        {
            EnemyBehaviour EnemyScript = other.GetComponent<EnemyBehaviour>();
            EnemyScript.TakeDamage(damage);
        }
    }

    public void TakeDamage(float damage) //PlayerTakingDamage
    {
        playercurrenthealth -= damage;
        if (playercurrenthealth <= 0)
        {
            Destroy(this.gameObject);
        }
    }

    public void SetMaxHealth()
    {
        playercurrenthealth = playermaxhealth;
    }

    public void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.blue;
        Gizmos.DrawWireSphere(transform.position, attackrange);
    }
}

I’m assuming the enemy Transform field in this script is the enemy GameObject that’s being destroyed.
When you destroy the enemy GameObject, the reference to enemy becomes null.

The typical fix is to check if(enemy != null) before doing anything with it, but the way you structured this makes it look like there’s supposed to be only one enemy in the game, is that correct?
If so, can it be assumed that the game is supposed to end when the enemy gets destroyed? Because you may not need to bother with null-checking if that’s the case. You could just end the game right when the enemy dies.

If that’s not the case, and there are supposed to be multiple enemies for the player to encounter in the game, then this script won’t really handle that. It’s only getting a reference to one random enemy when it loads.

1 Like

oop I didn’t know that gonna cause the game to end when one enemy get killed but I need to learn more if(enemy != null) statement first.

No, it won’t cause the game to end; I’m saying that if your game only has one enemy, and the game should end when that enemy is destroyed, then there’s no need to add null-checking, because you would just end the game after the enemy is destroyed.

If the game isn’t supposed to end when the enemy is destroyed, then yes, you will need to check if the enemy still exists before doing any logic with it.
Either way, the game won’t just magically “end” on its own when a GameObject is destroyed; you have to explicitly create some way for the game to end.

The other thing I was pointing out is that, if there are supposed to be multiple enemies that the player has to deal with, then this script doesn’t make much sense, since it’s just getting the reference to one random enemy in the scene and comparing its distance to the player.
Once that one random enemy is destroyed, that distance check will stop running. If that’s intentional, then there’s no problem; I just wanted to make sure that it is, indeed, intentional.

1 Like

I want to make this game like battle cat/line ranger