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 Ochreous · Sep 29, 2012 at 02:05 AM · c#

How to manipulate two buttons with one for loop C#

Hi everyone, can you manipulate two buttons with one for loop? From what it looks like I can only use a for loop once and then you will have to create another for loop for the second button.Otherwise I get an error i does not exist in it's current context.

     [System.Serializable]
 
     public class ButtonValue
     {
         public int IntValue = 0;
     }
 public List<ButtonValue> Buttons = new<ButtonValue>(4);
 
 Void OnGUI(){
  for (int i = 0; i < Buttons.Count i++)
                 if(GUILayout.Button("Button1"GUILayout.Width(125), GUILayout.Height(25)){
 ++Buttons[i]
 }
 //error i does not exist in it's current context
 if(GUILayout.Button("Button2"GUILayout.Width(125), GUILayout.Height(25)){
 ++Buttons[i]
 }                
 }
Comment
Add comment · Show 3
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 kmeboe · Sep 29, 2012 at 03:12 AM 0
Share

I don't want to sound too rude....your code sample is a complete train wreck. Seriously. There are like 15 separate errors in there, and I know you know how to do this stuff. Fix it!

Did that come across rude? It felt rude. Apologies in advance. Or rather in arrears.

avatar image Ochreous · Sep 29, 2012 at 04:19 AM 0
Share

I'm not getting any errors from the console.Could you please be a bit more specific?

avatar image kmeboe · Sep 29, 2012 at 07:12 AM 0
Share

Sure, I'll list the first five here:

1) You have a closing brace immediately after your first declaration (public int...), ending the class. There should be no brace there.

2) public List Buttons = new(4); -- needs to have "List" -- as in "new List..."

3) "for (int i = 0; i < Buttons.Count i++)" -- needs to have a semi-colon after "Count"

4 and 5) "if(GUILayout.Button("Button1"GUILayout.Width(125), GUILayout.Height(25)){" -- needs to have a comma after "Button1". Also needs an extra right paren at the end, before the brace.

6) "++Buttons[i]" needs a semi-colon after it

...and there are more. If you copy and paste the code from your question into a new c# file, you should see all of these errors (well, one at a time as you fix them). I wonder if it's a problem with the way you're copying and pasting your code here?

1 Reply

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

Answer by MarkFinn · Sep 29, 2012 at 03:45 AM

You need to be sure that the code using the variable i is inside the for block.

Start by adding a { to the end of the line

for (int i = 0; i < Buttons.Count i++)

otherwise only the first statement after it is in the for loop.

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 Ochreous · Sep 29, 2012 at 04:25 AM 0
Share

I didn't know it was that simple. Thanks for the help.

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

12 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

Related Questions

C# SphereCast isn't Inaccurate 1 Answer

C# GameObject is not detecting collision with Character Controller 2 Answers

FPS keep a loadout 0 Answers

Converting C# Controller from WASD Controls to Click-Based 1 Answer

C# GUI.Tooltip If Statement 2 Answers


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