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){}
}
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?
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";
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.
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?
That was ButtonInfo before I changed it. I may have forgotten to change some lines. Thanks for the help.
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 ;)
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 */}})
Your answer
Follow this Question
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