Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by shining_gentleman · Aug 07, 2019 at 01:36 PM · gameobjectbooleanreference

Bool's value won't return to false, why?

I know this sounds like a simple problem, but I can't find the solution no matter what. I just simply wanna do something like this:

 public class Player: Monobehaviour
 {
 
 public GameObject targetEnemy;
 private bool inCombat;
 
 void Update ()
     {
         if (targetEnemy != null)
         {
             inCombat = true;
         } else
         {
             inCombat = false;
         }
     }
 }

And here is the method that will return the targetEnemy:

 public class EnemySensor: Monobehaviour
 {
 
 private List<GameObject> interactors = new List<GameObject>();
     private Player player;
 
     void Start()
     {
         player = transform.parent.gameObject.GetComponent<Player>();
     }
 
     void Update()
     {
         player.targetEnemy = GetClosestEnemy ();
     }
 
 public GameObject GetClosestEnemy()
     {
         if (interactors.Count != 0)
         {
             GameObject closest = null;
             float minDistance = Mathf.Infinity;
             foreach (GameObject enemy  in interactors)
             {
                 float distance = Vector3.Distance (enemy.transform.position, player.transform.position);
                 if (distance < minDistance)
                 {
                     closest = enemy;
                     minDistance = distance;
                 }
             }
             return closest;
         } else
         {
             return null;
         }
     }
 }

Everything work just fine, when some NavMesh enemies entering my EnemySensor trigger at once, the script adds them to interactors List, and removes them on trigger exit. After that the GetClosestEnemy() method returns the closest enemy to Player's targetEnemy. The targetEnemy field which is visible in inspector showing an Enemy GameObject when closest enemy got returned, and None (GameObject) when no enemies are currently being detected by EnemySensor. But my inCombat bool stays true all the time even when targetEnemy field showing "None (GameObject)." Help? Many thanks in advance!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by I_Am_Err00r · Aug 07, 2019 at 02:16 PM

Just a suggestion, but try moving this:

  foreach (GameObject enemy  in interactors)
              {
                  float distance = Vector3.Distance (enemy.transform.position, player.transform.position);
                  if (distance < minDistance)
                  {
                      closest = enemy;
                      minDistance = distance;
                  }
              }
              return closest;

to something like this:

  foreach (GameObject enemy  in interactors)
              {
                  float distance = Vector3.Distance (enemy.transform.position, player.transform.position);
                  if (distance < minDistance)
                  {
                      closest = enemy;
                      minDistance = distance;
                      return closest;
                  }
              }
              else
              {
                  return null;
              }

I don't have a compiler to confirm if that would work, but I see you returning closest outside of your distance check in the foreach loop which I think is causing the issue; so the way I'm thinking is if you run that distance check, and an enemy is less than the max distance, you return that enemy there, or if there is nothing within that distance, you return null after that distance check.


Again, don't have Unity or a compiler, so please check for syntax errors and you might need to move the 'return null;' around to get it just right (maybe add another, I'm not a whiz at coding and can't say definitively without testing if my solution returns all paths). Let me know if I'm in the ballpark to helping you solve this!

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 shining_gentleman · Aug 07, 2019 at 02:38 PM 0
Share

Unfortunately, I got a compiler error "not all code paths return a value" by doing this

avatar image I_Am_Err00r shining_gentleman · Aug 07, 2019 at 02:47 PM 0
Share

This line:

I'm not a whiz at coding and can't say definitively without testing if my solution returns all paths

I'll help you, but learn how to look stuff up on your own so that you can fix a simple error like this yourself:

 public GameObject GetClosestEnemy()
      {
          if (interactors.Count != 0)
          {
              GameObject closest = null;
              float $$anonymous$$Distance = $$anonymous$$athf.Infinity;
              foreach (GameObject enemy  in interactors)
              {
                  float distance = Vector3.Distance (enemy.transform.position, player.transform.position);
                  if (distance < $$anonymous$$Distance)
                  {
                      closest = enemy;
                      $$anonymous$$Distance = distance;
                     return closest;
                  }
              }
               else
               {
                   return null;
               }
          }
      }
      else
           {
              return null;
           }
  }

I might have the placement wrong (can't confirm without a compiler) and I'm literally writing this on the Unity forum, so please move that last else {return null;} around to find out what path doesn't return a value.

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

164 People are following this question.

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

Related Questions

Reference a script on a gameobject that is disabled in scene 0 Answers

How to reference a GameObjects Box Collider true in a if statement! 2 Answers

Addressables' references - right object is there, but not! 1 Answer

Set object reference for script at runtime 0 Answers

How can I get an object reference from a Raycast? 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