Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by user-12161 (google) · May 25, 2012 at 06:49 PM · c#nullstrategy

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lo0NuhtiK · May 25, 2012 at 06:52 PM 2
Share

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

avatar image Sirex · May 26, 2012 at 09:32 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by GibTreaty · May 25, 2012 at 06:59 PM

 GameObject target = GetClosestTarget(30);
     
 if(target != null) {
     //Do stuff
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image user-12161 (google) · May 26, 2012 at 08:44 AM 0
Share

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.

avatar image Bunny83 · May 26, 2012 at 10:06 AM 0
Share

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...

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges