• 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 Ochreous · Sep 24, 2012 at 12:21 AM · c#gui

How to create an if statement for each element within a list C#

Hi everyone, Is there a way to create an if statement for each element within a list? I want each of my buttons do something different but I 'm not sure how to set that up.

 public List<ButtonInfo> ListOfButtons;
    public class Buttoninfo
         {
         
             public int ButtonFunction = 0;
         }
 Void OnGUI(){
                for (int i = 0; i < ListOfButtons.Count; i++)
                 if (GUILayout.Button("Button", GUILayout.Width(125), GUILayout.Height(25)))
                 {
                     Buttoninfo[i].Value++;
                 }
 //This is all I could come up with. It's incredibly inefficient and I would Like to change it 

             if(ListOfButtons[0].ButtonFunction == 1){}
         if(ListOfButtons[1].ButtonFunction == 1){}
         if(ListOfButtons[2].ButtonFunction == 1){}
         if(ListOfButtons[3].ButtonFunction == 1){}
         if(ListOfButtons[4].ButtonFunction == 1){}
         if(ListOfButtons[5].ButtonFunction == 1){}
 }
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 Bunny83 · Sep 24, 2012 at 12:31 AM 0
Share

Well, it totally depends on what you actually want to do for each button. When it can't be generalized in some way, your way is the only one. What do you want to execute? Can you give us some examples?

avatar image Ochreous · Sep 24, 2012 at 12:58 AM 0
Share

Here is what I was planning to do. I was going to create a string variable and if one of the if statements was true the string variable would be equal to something. Something like this

public string Text

     if(ListOfButtons[0].ButtonFunction == 1){}

    Text = "Blah";

    if(ListOfButtons[1].ButtonFunction == 1){}

    Text = "Blah2";

    if(ListOfButtons[2].ButtonFunction == 1){}

    Text = "Blah3";

    if(ListOfButtons[3].ButtonFunction == 1){}

    Text = "Blah4";

    if(ListOfButtons[4].ButtonFunction == 1){}

    Text = "Blah5";

    if(ListOfButtons[5].ButtonFunction == 1){}

   Text = "Blah6";
avatar image Owen-Reynolds · Sep 24, 2012 at 03:40 AM 0
Share

It looks like you're using these as radioButtons -- turning one ON turns the rest off, so you are selecting one string.

If that's the case, all you need is a single int, like `currentButtonNum`. Could also look at `GUI.toggle`, which can display them a "turned on" looking button.

2 Replies

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

Answer by Bunny83 · Sep 24, 2012 at 01:15 AM

Well, in the case you mentioned in your comment, you can just use an array with your strings. Set the strings either as "constants" of make the array public so you can set them in the inspector:

 private string[] texts = new string[] {"Blah", "Blah2", "Blah3", "Blah4", "Blah5", "Blah6"};
 // or
 public string[] texts; // set them in the inspector
 
 // [...]
 for (int i = 0; i < ListOfButtons.Count; i++)
 {
     if(ListOfButtons[i].ButtonFunction == 1){}
     {
         Text = texts[i];
     }
 }

ps. I'm a bit confused. You have the List "ListOfButtons" which holds ButtonInfo instances, but what is "buttonsList"? Does it's elemets corresponds to the ListOfButtons elements? Why not just one list?

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 Ochreous · Sep 24, 2012 at 01:35 AM 0
Share

That was ButtonInfo before I changed it. I may have forgotten to change some lines. Thanks for the help.

avatar image Bunny83 · Sep 24, 2012 at 01:41 AM 0
Share

But if "buttonsList" and "ListOfButtons" is the same thing, where does the "Value" come from? It seems you messed up your code a lot. $$anonymous$$aybe you can edit your question and fix it ;)

avatar image
0

Answer by Simon-O · Nov 29, 2016 at 12:05 PM

This is pretty trivial using LINQ:

 using System.Linq;
 
 ListOfButtons.ForEach(x => {if(x.ButtonFunction == 1) { /* Do something with button x */}})
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 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

Related Questions

C# Rigidbody Rotation Resets 1 Answer

C# can't see Dictionary in Inspector 3 Answers

NullReferenceException 0 Answers

Display list as UI or GUI 0 Answers

Instantiated in IF statement - Else Reference object 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges