Stop part of code after player die

Hi guys, so im working on a prototype ai for my game all working fine but i have a problem, if the player die, i get this erro in console:

MissingReferenceException: The object of type ‘Player’ 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.
RangedEnemy.IsInRange (Single range) (at Assets/Scripts/Enemy/RangedEnemy.cs:88)
RangedEnemy.Attack () (at Assets/Scripts/Enemy/RangedEnemy.cs:60)
RangedEnemy.Update () (at Assets/Scripts/Enemy/RangedEnemy.cs:27)

I know that the enemies after player die keep trying to find the player so i get the error, but i dont get how to fix it… The error indicate is at line 88, so i have to stop the game to do that comparation… i try add " & curHealth >0", and other things but dont work. Can anyone help me plz?? Ty

using UnityEngine;
using System.Collections;

public class RangedEnemy : Enemy
{
    Player player;
   
    private NavMeshAgent navAgent;

    bool blockEnemy;

    public GameObject fireballPrefab;

    // Use this for initialization
    void Start () {
        player = Player.player;
       
        curHealth = maxHealth;
       
        navAgent = GetComponent<NavMeshAgent>();

        blockEnemy = false;
    }
   
    // Update is called once per frame
    void Update () {
        Attack();
       
        FollowPlayer();
    }
    void FollowPlayer()
    {
        if (player.curHealth > 0)
        {
            if (IsInRange (chasingRange))
            {
                if (!IsInRange (attackRange))
                {
                    navAgent.SetDestination (player.transform.position);
               
                    navAgent.Resume ();
                }
            }
            else
            {
                navAgent.SetDestination (transform.position);
            }
        }
        else if (player.curHealth <= 0)
        {

        }
    }
   
    void OnMouseOver(){
        Player.opponent = transform;
    }
   
    protected override void Attack(){
        if (IsInRange (attackRange)) {
            if(!blockEnemy)
            {
                if(player.curHealth > 0)
                {
                    GameObject fireball = Instantiate(fireballPrefab, transform.position, transform.rotation) as GameObject;

                    fireball.GetComponent<PrefabController>().target = player.transform;
                           
                    blockEnemy = true;
                           
                    Invoke("UnBlock", attackSpeed);
                }
                else if(player.curHealth <= 0)
                {

                }
            }
        }
    }
   
    void UnBlock()
    {
        blockEnemy = false;
    }
   
    bool IsInRange(float range)
    {
        if (Vector3.Distance (player.transform.position, transform.position) < range)
        {           
            return true;
        }
        else
        {
            return false;
        }
    }
}

Just do a null check before you do anything with the player:

bool IsInRange(float range)
   {
if (player != null)
{
       if (Vector3.Distance (player.transform.position, transform.position) < range)

In that case you’ll probably want to put “else return false;” at the end of the function too.

i try this but i get this error:

Assets/Scripts/Enemy/RangedEnemy.cs(86,14): error CS0161: `RangedEnemy.IsInRange(float)': not all code paths return a value

bool IsInRange(float range)
    {
        if (player != null)
        {
            if (Vector3.Distance (player.transform.position, transform.position) < range)
            {           
                return true;
            }
            else
            {
                return false;
            }
        }
    }

Read the part after the code block I posted

1 Like

i thing ur talking of the return false i already have, sry m8. it works perfect O appreciate. :wink: