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 /
avatar image
0
Question by putlucky · Apr 19, 2017 at 09:37 PM · raycastenemybooleanselection

Boolean being overwritten

So I have a method that checks to see if you clicked on an 'enemy' gameobject. I want to be able to deselect the enemy by clicking away, say on some terrain, and I tried doing this with booleans.

        if (Input.GetMouseButtonDown(1))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             foreach (string enemy in enemyTitles)
             {
                 if (Physics.Raycast(ray, out hit) && target != null)
                 {
                     target.transform.position = hit.point;
 
                     //Check to see if you right-clicked on an enemy from the array.
                     if (hit.transform.gameObject.tag == enemy)
                     {
                         enemySelected = true;
                         Debug.Log("engaging now." + enemy + "" + enemySelected);
                         //Set global variable to the selected enemy's tag. 
                         selectedEnemy = enemy;
                         chosenEnemy = hit.transform.gameObject;
                         target.transform.position = chosenEnemy.transform.position;
 
                     }
                     else if (hit.transform.gameObject.tag != enemy){enemySelected = false;}
                     
                 
                 }
                
             }
             
         }

The problem is, enemySelected is true only for a single frame, as the next frame overwrites it, I want it to be true UNTIL I click away. I was wondering what the best way to handle this would be?

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 Bunny83 · Apr 19, 2017 at 09:47 PM

How do you know it's true for one frame? Your code most likely will reset the variable during the same frame. You iterate through all "enemyTitles" and "enemySelected" will only be true when the object you hit is the last item in that list / array. Because each iteration you either set your variable true or false. So say you have 5 items in your list and the object you hit is the 3rd item. When the loop gets to the 3rd element you set it to true. However when you continue iterating it will set it back to false at item 4 and 5 as it doesn't match that name.

To select a single object you want to:

  • reset the variable to false "once" before you start the loop.

  • terminate the loop when you found a match.

  • Also you only need to do one raycast.

Something like that:

 if (Input.GetMouseButtonDown(1))
 {
     RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     // reset it here
     enemySelected = false;
     if (Physics.Raycast(ray, out hit) && target != null)
     {
         target.transform.position = hit.point;
         foreach (string enemy in enemyTitles)
         {
             if (hit.transform.gameObject.tag == enemy)
             {
                 enemySelected = true;
                 // [ ... ]
                 break; // stop the for loop
             }
             // [ ... ]





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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Best way for me to deselect something once it's selected? 0 Answers

Making bullet holes in enemies. 1 Answer

How to determine direction in order to compare it to raycast results 0 Answers

Enemy to move out of way of player 1 Answer

Can else statements be nested inside an else statement? 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