|
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) and this is the functions i have for finding enemies and resources: thanks for reading :)
(comments are locked)
|
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; 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)
|
|
You have several things which could break your code:
(comments are locked)
|

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.