Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

How can I simplify this code?

Discussion in 'Scripting' started by makizume, Jul 16, 2019.

  1. makizume

    makizume

    Joined:
    May 9, 2018
    Posts:
    4
    Hi All
    I was wondering if you guys can help me simplify my code(I am a beginner of programming.)
    what I want to achieve here is accordingly.

    [problem]
    I wrote a switch statement but they are pretty long, and if I add more characters to select, it is not efficient to add like that.
    basically, if a certain character is on, other 3 or more character will be setActive to false at the same time with a few lines of code. by the way, these characters are a child object of the parent object.

    Thank you

    [character select UI for mobile]
    1. I put 4 buttons UI in Unity for selecting characters
    2. each button has ID numbers
    3. if a certain character is on, other characters are off.

    Code (CSharp):
    1. public class characterManager : MonoBehaviour
    2. {
    3.     public GameObject[] newCharacter;
    4.  
    5.     void Start()
    6.     {
    7.         newCharacter = GetComponentsInChildren<GameObject>();
    8.     }
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.       switch(characterSelect.playerNumber)
    14.         {
    15.          
    16.  
    17.             case 0:
    18.                 newCharacter[characterSelect.playerNumber].SetActive(true);
    19.                 newCharacter[1].SetActive(false);
    20.                 newCharacter[2].SetActive(false);
    21.                 newCharacter[3].SetActive(false);
    22.  
    23.                 break;
    24.             case 1:
    25.                 newCharacter[characterSelect.playerNumber].SetActive(true);
    26.                 newCharacter[0].SetActive(false);
    27.                 newCharacter[2].SetActive(false);
    28.                 newCharacter[3].SetActive(false);
    29.  
    30.                 break;
    31.             case 2:
    32.                 newCharacter[characterSelect.playerNumber].SetActive(true);
    33.                 newCharacter[0].SetActive(false);
    34.                 newCharacter[1].SetActive(false);
    35.                 newCharacter[3].SetActive(false);
    36.  
    37.                 break;
    38.             case 3:
    39.                 newCharacter[characterSelect.playerNumber].SetActive(true);
    40.                 newCharacter[0].SetActive(false);
    41.                 newCharacter[1].SetActive(false);
    42.                 newCharacter[2].SetActive(false);
    43.  
    44.                 break;
    45.                
    46.         }
    47.     }
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    364
    Seems like you want a for loop.
    Code (csharp):
    1. void Update()
    2. {
    3.     for(int i = 0; i < newCharacter.Length; i++) // Starting from 0, loop through each element of the array
    4.     {
    5.         newCharacter[i].SetActive(i == characterSelect.playerNumber); // set the object active only if i == our selected number.
    6.     }
    7. }
     
    makizume likes this.
  3. makizume

    makizume

    Joined:
    May 9, 2018
    Posts:
    4
    OMG THANK YOU SOOOOOOO MUCH WallaceT_MFM! You are so genius!
    I really appreciate it so much. Wow, I really need to practice until I can be close to your level :)
    from now on, I will use this great code to my UI all the time. Thank you from Japan!!!!!
     
    StarManta and WallaceT_MFM like this.
unityunity