- Home /
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 :)
Check if it's null. if it's not null, do this. else, do that.
Use C idom. Pass a reference to a GameObject then have teh funciton return true or false if it finds anything.
Answer by GibTreaty · May 25, 2012 at 06:59 PM
GameObject target = GetClosestTarget(30);
if(target != null) {
//Do stuff
}
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.sqr$$anonymous$$agnitude;
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 = $$anonymous$$athf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos) {
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqr$$anonymous$$agnitude;
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.
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 ins$$anonymous$$d AttackTarget which isn't even used to call that function...
Answer by Bunny83 · May 26, 2012 at 10:18 AM
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
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
2D Pathfinding: how to create navMesh 0 Answers
Setting Prefab with Code Returns NULL 2 Answers
UnityEngine Assert IsNotNull not working on empty inspector field 1 Answer