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]
- I put 4 buttons UI in Unity for selecting characters
- each button has ID numbers
- if a certain character is on, other characters are off.
public class characterManager : MonoBehaviour
{
public GameObject[] newCharacter;
void Start()
{
newCharacter = GetComponentsInChildren<GameObject>();
}
// Update is called once per frame
void Update()
{
switch(characterSelect.playerNumber)
{
case 0:
newCharacter[characterSelect.playerNumber].SetActive(true);
newCharacter[1].SetActive(false);
newCharacter[2].SetActive(false);
newCharacter[3].SetActive(false);
break;
case 1:
newCharacter[characterSelect.playerNumber].SetActive(true);
newCharacter[0].SetActive(false);
newCharacter[2].SetActive(false);
newCharacter[3].SetActive(false);
break;
case 2:
newCharacter[characterSelect.playerNumber].SetActive(true);
newCharacter[0].SetActive(false);
newCharacter[1].SetActive(false);
newCharacter[3].SetActive(false);
break;
case 3:
newCharacter[characterSelect.playerNumber].SetActive(true);
newCharacter[0].SetActive(false);
newCharacter[1].SetActive(false);
newCharacter[2].SetActive(false);
break;
}
}