x


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 :)

more ▼

asked May 25 '12 at 06:49 PM

user-12161 (google) gravatar image

user-12161 (google)
12 2 3 7

Check if it's null. if it's not null, do this. else, do that.

May 25 '12 at 06:52 PM Lo0NuhtiK

Use C idom. Pass a reference to a GameObject then have teh funciton return true or false if it finds anything.

May 26 '12 at 09:32 AM Sirex
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
GameObject target = GetClosestTarget(30);

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

answered May 25 '12 at 06:59 PM

GibTreaty gravatar image

GibTreaty
199 1 4 6

this is what i have in my update.

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;
}

so yeah that solution doesn't exactly work for me. thanks for replying.

May 26 '12 at 08:44 AM user-12161 (google)

What do you mean it doesn't wotk exactly for you? What i can see is that you call your Attach function with the target variable as parameter, but you don't check if target is null. You check instead AttackTarget which isn't even used to call that function...

May 26 '12 at 10:06 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

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
more ▼

answered May 26 '12 at 10:18 AM

Bunny83 gravatar image

Bunny83
45.2k 11 49 207

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4146
x194
x32

asked: May 25 '12 at 06:49 PM

Seen: 499 times

Last Updated: May 26 '12 at 10:18 AM