• 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 abstractfragment · Jun 19, 2018 at 01:49 AM · arraysloops

Trying to get the number of buttons being stood on in a scene.

Want a door to constantly check whether all the buttons in a scene are being held down. I think I might be going about this all wrong. This is done in Update()

 foreach (GameObject button in buttons){
             buttonScriptARR thisButton = button.GetComponent<buttonScriptARR> ();
             if (thisButton.isButtonDown == true) {
                 for (int i = 0; i < buttons.Length; i++) {
                     numberOfButtonsDown++;
             
                 }
             }
             else if (thisButton.isButtonDown == false){
                 for (int i = 0; i < buttons.Length; i++)
                     numberOfButtonsDown--;
 
                     }
         }
     }

I don't know a good way to end the loop, what happens now is standing on the button causes the numberOfButtonsDown to increase toward infinity and vice versa for not standing on it.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by abstractfragment · Jun 19, 2018 at 05:47 PM

Solved this myself, didn't need the foreach loop and for loops. Also added script to reset the number of buttons held down to zero at the beginning of each fixed update.

 void Update () {
         numberOfButtonsDown = 0;
         Debug.Log ("Rest take");
         
         foreach (GameObject button in buttons){
             buttonScriptARR thisButton = button.GetComponent<buttonScriptARR> ();
             if (thisButton.isButtonDown == true) {
                 {
                     
                     numberOfButtonsDown++;
                     Debug.Log ("down take" + numberOfButtonsDown);
             
                 }
             }
             else if (thisButton.isButtonDown == false){
                     
                     
                 Debug.Log ("up take" + numberOfButtonsDown);
 
                     }
             if (numberOfButtonsDown < 0) {
                 numberOfButtonsDown = 0;
             }
         }
     }
 }
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
avatar image
0

Answer by top2323 · Jun 19, 2018 at 04:08 AM

What you might do is have an int variable in your button script, 1 if held, 0 if not, and then in your foreach you could go through every button script, grab it, and add it into a central counter.

Comment
Add comment · Show 1 · 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 abstractfragment · Jun 19, 2018 at 05:48 PM 0
Share

Did a variation of this, thanks to your help though I looked at counters and found a similar solution.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

86 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

Related Questions

Instantiate for each in array 1 Answer

Make a GameObject changes between 2 colors with a time wait 1 Answer

Problem with arrays and while loops 0 Answers

Argument if out of range: loops and arrays 2 Answers

create an array of unknown size in a loop 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