Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

any simple way to check something in the list?

Discussion in 'Scripting' started by Cyrus-lam, Apr 9, 2016.

  1. Cyrus-lam

    Cyrus-lam

    Joined:
    Mar 1, 2016
    Posts:
    7
    Hello all,
    I'm new for scriupting so I have a question about how to simplify my script
    So i have a list named Char1AttackCommand[ ], my intention is base on the list if it match to some key word then change the color :

    Code (CSharp):
    1.  void AssignCommandColor(int arrayNumber, string CharXAttackCommand)
    2.     {
    3.        if(IsCharacter1 == true)
    4.         {
    5.             if (BPM.Char1AttackCommand[1] == "Fire")
    6.             {
    7.                 UICommandReadyBar.color = Color.red;
    8.             }else if (BPM.Char1AttackCommand[1] == "Water")
    9.             {
    10.                 UICommandReadyBar.color1 = Color.blue;
    11.             }
    12.             else if (BPM.Char1AttackCommand[1] == "Wind")
    13.             {
    14.                 UICommandReadyBar.color = Color.green;
    15.             }
    16.             else if (BPM.Char1AttackCommand[1] == "Dark")
    17.             {
    18.                 UICommandReadyBar.color = Color.black;
    19.             }
    20.             else if (BPM.Char1AttackCommand[1] == "Light")
    21.             {
    22.                 UICommandReadyBar.color = Color.yellow;
    23.             }
    Code (CSharp):
    1.   if(IsCharacter2 ==true)
    2.         {
    3.             if (BPM.Char1AttackCommand[2] == "Fire")
    4.             {
    5.                 UICommandReadyBar.color = Color.red;
    6.             }else if (BPM.Char1AttackCommand[2] == "Water")
    7.             {
    8.                 UICommandReadyBar.color1 = Color.blue;
    9.             }
    10.             else if (BPM.Char1AttackCommand[2] == "Wind")
    11.             {
    12.                 UICommandReadyBar.color = Color.green;
    13.             }
    14.             else if (BPM.Char1AttackCommand[2] == "Dark")
    15.             {
    16.                 UICommandReadyBar.color = Color.black;
    17.             }
    18.             else if (BPM.Char1AttackCommand[2] == "Light")
    19.             {
    20.                 UICommandReadyBar.color = Color.yellow;
    21.             }
    I need to do it for all the list, but i dont want to do it manually. may i know any way i can simplify it please :)
    may thanks
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Also, have a look at loops (especially for/foreach loops in your case) in combination with arrays or any type of collection and wrap your head around enumerations. Latter ones would replace your strings and guarantee that there's no typo and will be more efficient.
     
  4. Cyrus-lam

    Cyrus-lam

    Joined:
    Mar 1, 2016
    Posts:
    7
    thanks for reply,
    I have look at the for each loop, but dont really know how to make my logic work (sorry about the silly question.)
    Code (CSharp):
    1. foreach (string i in BPM.Char1AttackCommand)
    2.             {
    3.                  //Idont know what should i put it here
    4.             }