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
0
Question by Ochreous · May 27, 2013 at 07:37 PM · c#ifstatement

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))){
 
             }
        }
     }
 }
Comment
Add comment · Show 1
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 numberkruncher · May 27, 2013 at 08:01 PM 0
Share

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 {
 }

3 Replies

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

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;
     }
 }
Comment
Add comment · Show 4 · 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 · May 27, 2013 at 08:26 PM 0
Share

What about other stuff like if I needed to work with a boolean or string?

avatar image numberkruncher · May 27, 2013 at 08:31 PM 0
Share

You really need to be more specific since you haven't said what you are trying to achieve.

avatar image Ochreous · May 27, 2013 at 08:42 PM 0
Share

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?

avatar image numberkruncher · May 27, 2013 at 09:03 PM 0
Share

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.
         }
     }
 }
avatar image
1

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;
 }
Comment
Add comment · Show 3 · 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 · May 27, 2013 at 07:56 PM 0
Share

Is there a way to call a switch statement? I need something that will work for the way I have my buttons setup.

avatar image robertbu · May 27, 2013 at 08:01 PM 0
Share

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);

avatar image Ochreous · May 27, 2013 at 08:16 PM 0
Share

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];
             }
        }
     }
 }
 
avatar image
0

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 ();
     }
 }
 
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 · May 27, 2013 at 09:02 PM 0
Share

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 ();
     }
     }
 
avatar image Hotshot10101 · May 27, 2013 at 10:59 PM 0
Share

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

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

16 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

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


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