C# how to ingore null return

Hi, i’m trying to make a strategy game in the style of warcraft, and i have ran into this problem while programming my basic unit. i have to functions one that returns the nearest (with the maximun range of 30) GameObject that has the Unit script and has a different tag , and the other returns the nearest GameObject that has the one of the following tags “Forest”, “Mine”, “Farm”, “Boulder”. the problem is that the script stops because the two functions return null witch is supposed to happen, but i want the script to ignore the ‘returned null’ error and continue executing. how do i do that ?

edit(moved from comment, reformatted)

if(AttackTarget!=null)
    Attack(target);
else if(TargetResource!=null)
    Gather("Farm");

if(target==null || TargetResource==null)
{
    if(FindEnemy()!=null)
    {
        target = FindEnemy();
    }
    else if(FindNearestWithTag("Farm")!=null)
    {
        TargetResource = FindNearestWithTag("Farm");
    }
    else
    {
        if(Vector3.Distance(transform.position,myBase.Destination)>5f)
        {
            transform.LookAt(myBase.Destination); velocity = speed; }else{ velocity = 0;} } }

and this is the functions i have for finding enemies and resources:

Transform FindEnemy()
{
    Unit[] gos = FindObjectsOfType(typeof(Unit)) as Unit[];
    GameObject closest = null;
    float distance = myBase.teritory;
    Vector3 position = transform.position;
    foreach (Unit go in gos)
    {
        Vector3 diff = go.gameObject.transform.position - position;
        float curDistance = diff.sqrMagnitude;
        if (curDistance < distance && go.gameObject.tag!=this.gameObject.tag)
        {
            closest = go.gameObject;
            distance = curDistance;
        }
    }
    return closest.transform;
}

Transform FindNearestWithTag(string target)
{
    GameObject[] gos = GameObject.FindGameObjectsWithTag(target);
    GameObject closest = null;
    float distance = Mathf.Infinity;
    Vector3 position = transform.position;

    foreach (GameObject go in gos)
    {
        Vector3 diff = go.transform.position - position;
        float curDistance = diff.sqrMagnitude;
        if(go.tag=="Farm")
        {
            if (curDistance < distance&&go.GetComponent<Farm>().isEmpty==false)
            {
                closest = go;
                distance = curDistance;
            }  
        }
        else
        {
            if (curDistance < distance)
            {
                closest = go;
                distance = curDistance;
            }
        } 
    }
    return closest.transform;
}

thanks for reading :slight_smile:

GameObject target = GetClosestTarget(30);

if(target != null) {
    //Do stuff
}

You have several things which could break your code:

  • Your FindEnemy function’s last line will produce a null-ref-exception when there is no enemy in range. return closest.transform when closest is null you can’t access it’s transform…
  • Same thing for FindNearestWithTag
  • You call your Attach function without checking the target value for null