- Home /
C# Creating a List of If Statements
Hi everyone, I want to put a list of if statements into a format similar to the way I have my buttons. But I'm not sure how. I would like to declare a bunch of them in a list but I don't think you can make a list of if statements.
using UnityEngine;
using System.Collections;
public class SomeGUI : MonoBehaviour {
public string[] someText;
void OnGUI () {
someText= new string[] {"Text1","Text2","Text3","Text4","Text5","Text6"};
for(int i = 0; i < someText.Length; i++){
//list of if statements
if(GUILayout.Button(someText[i], GUILayout.Width(142), GUILayout.Height(25))){
}
}
}
}
Please be more specific, what are you trying to achieve exactly?
Perhaps you are just after the else if
statement?
if (a) {
}
else if (b) {
}
else {
}
Answer by numberkruncher · May 27, 2013 at 08:22 PM
In that case I think you are looking for something like the following:
for (int i = 0; i < someText.Length; ++i) {
if (GUILayout.Button(someText[i], GUILayout.Width(142), GUILayout.Height(25))) {
// If button was clicked change value of someInt (aka index of button).
someInt = i;
}
}
What about other stuff like if I needed to work with a boolean or string?
You really need to be more specific since you haven't said what you are trying to achieve.
I want a switch statement or if statement that works for with a for loop. I want to make it so it will run done several if statements and use the if statement that corresponds with which ever button was pressed. Something like this.
using UnityEngine;
using System.Collections;
public class SomeGUI : $$anonymous$$onoBehaviour {
public string[] someText;
void OnGUI () {
someText= new string[] {"Text1","Text2","Text3","Text4","Text5","Text6"};
for(int i = 0; i < someText.Length; i++){
if(GUILayout.Button(someText[i], GUILayout.Width(142), GUILayout.Height(25))){
if(if statement[i]){
}
}
}
}
}
Does that make sense?
You simply place your button-clicked logic inside the if
statement that surrounds your button:
// Display "Hello", "World", "Hello", "World" buttons (4 buttons).
for (int i = 0; i < 2; ++i) {
if (GUILayout.Button( "Hello" )) {
// This will ONLY happen when this button is pressed.
if (i == 0) {
// First instance of "Hello" button.
}
else if (i == 1) {
// Second instance of "Hello" button.
}
}
if (GUILayout.Button( "World" )) {
// This will ONLY happen when this button is pressed.
if (i == 0) {
// First instance of "World" button.
}
else if (i == 1) {
// Second instance of "World" button.
}
}
}
Answer by robertbu · May 27, 2013 at 07:53 PM
You can use a switch statement:
switch(i) {
case 0 :
// Do for 0
break;
case 1:
// Do for 1
break;
case 2:
// Do for 2
break;
default:
// Do for everything else
break;
}
Is there a way to call a switch statement? I need something that will work for the way I have my buttons setup.
You can either just drop the switch statement into your code at line 14, or you can put it in a separate function:
void DoSwitch(int i) {
// Switch statment here.
}
And you would call at line 14 by:
DoSwitch(i);
That's not working for me. I'll set this up a bit differently. When one of the buttons is pressed someint will be equal to what the switch statement is set to. I've tried out this code and it doesn't work.
using UnityEngine;
using System.Collections;
public class SomeGUI : $$anonymous$$onoBehaviour {
public int[] someIntArray;
public int someint;
void OnGUI () {
someIntArray= new int[6];
for(int i = 0; i < someIntArray.Length; i++){
switch(i) {
case 0 :
someint = 1;
break;
case 1:
someint = 2;
break;
case 2:
someint = 3;
break;
case 3 :
someint = 4;
break;
case 4:
someint = 5;
break;
case 5:
someint = 6;
break;
}
if(GUILayout.Button(someIntArray[i].ToString(), GUILayout.Width(142), GUILayout.Height(25))){
someint = someIntArray[i];
}
}
}
}
Answer by Hotshot10101 · May 27, 2013 at 08:48 PM
If you don't mind getting a little fancy with .NET and C# you could do something like this:
using System;
using UnityEngine;
using System.Collections.Generic;
public class GUIScript : MonoBehaviour
{
public class TextButton
{
public Rect Position;
public string Text;
public Action Action;
}
List<TextButton> _textButtons;
// Use this for initialization
void Start ()
{
_textButtons = new List<TextButton> ();
_textButtons.Add (new TextButton { Position = new Rect (0, 0, 50, 50), Text = "text1", Action = () =>
{
// do whatever you would do when button 1 is hit
}});
_textButtons.Add (new TextButton { Position = new Rect (0, 75, 50, 50), Text = "text2", Action = () =>
{
// do whatever you would do when button 2 is hit
}});
}
// Update is called once per frame
void Update ()
{
}
void OnGUI ()
{
foreach (TextButton textButton in _textButtons)
if (GUI.Button (textButton.Position, textButton.Text))
textButton.Action ();
}
}
How do you get it to work? I can't get it to recognize my declared boolean somebool.
using System;
using UnityEngine;
using System.Collections.Generic;
public class GUIScript : $$anonymous$$onoBehaviour
{
public class TextButton
{
public Rect Position;
public string Text;
public Action Action;
}
List<TextButton> _textButtons;
public bool somebool;
// Use this for initialization
void Start ()
{
_textButtons = new List<TextButton> ();
_textButtons.Add (new TextButton { Position = new Rect (0, 0, 50, 50), Text = "text1", Action = () =>
{
//Somebool not recognized
}});
_textButtons.Add (new TextButton { Position = new Rect (0, 75, 50, 50), Text = "text2", Action = () =>
{
//Somebool not recognized
}});
}
// Update is called once per frame
void Update ()
{
}
void OnGUI ()
{
foreach (TextButton textButton in _textButtons)
if (GUI.Button (textButton.Position, textButton.Text))
textButton.Action ();
}
}
I don't understand what you mean by can't get it to recognize the boolean. You should have access to all member variables and local variable.
What is it you want to do with somebool?
Your answer
![](https://koobas.hobune.stream/wayback/20220613103022im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Saving final score and displaying on main menu 1 Answer
null texture passed to GUI.DrawTexture 0 Answers
How to Name Individual Buttons Within a List 2 Answers
c# Adjust In-Game audio 1 Answer
C# How to Detect Edges of a Collider 0 Answers