- Home /
How to apply switch case on the button to activate and deactivate game object?
//Collect the game object
public GameObject[] sorting;
//Collect the button
public GameObject[] sortingBtn;
int j;
int b;
void Start () {
sorting = GameObject.FindGameObjectsWithTag("Sort");
sortingBtn = GameObject.FindGameObjectsWithTag("SortButton");
for (j = 0; j < sorting.Length; j++)
{
Debug.Log(j + " " + sorting[j].name);
}
for (b = 0; b < sortingBtn.Length; b++)
{
Debug.Log(b + " " + sortingBtn[b].name);
}
Invoke("CurrentScene", 1);
}
public void CurrentScene()
{
switch (b)
{
case 1:
Debug.Log("Plastic hellooo");
sorting[1].SetActive(true);
sorting[0].SetActive(false);
break;
case 0:
Debug.Log("Paper hellooo");
sorting[0].SetActive(true);
sorting[1].SetActive(false);
break;
default:
Debug.Log("Default hellooo");
sorting[0].SetActive(true);
sorting[1].SetActive(false);
break;
}
}
The case default works well at the beginning. But when click the UI button to switch the case it doesn't work. How to make the button respond to the condition that has be set in switch case statement? (The button had been attached with pubic void CurrentScene in Unity).
the code you have right now seems to just count the total amount of buttons and change visibility based on that number. if you want to change things based on the clicked button you need to get references to the button script and add a function to the onclick. Is that what you want to achieve?
Yes, there is the thing I want to achieve. So what you means here is each of the buttons will refer to different function? Seem like the switch case won't be work well in this case.
Sorry for my bad English. :)
Let me write a quick answer that should work. expect a reply in a few $$anonymous$$utes
Answer by kuijerlattie · Mar 21, 2018 at 11:08 AM
//Collect the game object
public GameObject[] sorting;
//Collect the button
public GameObject[] sortingBtn;
int j;
int b;
void Start () {
sorting = GameObject.FindGameObjectsWithTag("Sort");
sortingBtn = GameObject.FindGameObjectsWithTag("SortButton");
for (j = 0; j < sorting.Length; j++)
{
Debug.Log(j + " " + sorting[j].name);
}
for (b = 0; b < sortingBtn.Length; b++)
{
Debug.Log(b + " " + sortingBtn[b].name);
Button btn = sortingBtn[b].GetComponent<Button>();
btn.onClick.AddListener(() => Currentscene(b));
}
Invoke("CurrentScene", 1);
}
public void CurrentScene(int buttonnumber)
{
switch (buttonnumber)
{
case 1:
Debug.Log("Plastic hellooo");
sorting[1].SetActive(true);
sorting[0].SetActive(false);
break;
case 0:
Debug.Log("Paper hellooo");
sorting[0].SetActive(true);
sorting[1].SetActive(false);
break;
default:
Debug.Log("Default hellooo");
sorting[0].SetActive(true);
sorting[1].SetActive(false);
break;
}
}
this should do the trick. if button is not available, put: using UnityEngine.UI; at the top of the file.
You can even remove the
Invoke("CurrentScene", 1);
and replace it with:
CurrentScene(-1);
to trigger the default case of the switch.
$$anonymous$$ay be I need to add up something?
Something miss out there, set the parameter to each button, as shown as figure above. And I delete the 2 rows code of button onclick. Then, it works well for me now. I think I should do some works in part of scripting to set value to the button ins$$anonymous$$d of just set the parameter of button in Unity even though it's a quite convenient way. :)
the image doesnt show, can you upload it to imgur or some other hosting site and link it here?
I'm trying it out. $$anonymous$$ay I ask how the -1 is actually works to trigger the function CurrentScene()? So far I have changed the code you suggest, the game start up like what I had set in case default, and when I click one of the buttons, nothing change. From the console, I can see that Debug.Log: Default hellooo and Paper hellooo come out at the same time when I click any one the buttons but the Plastic hellooo not even show out. Thank you for your help. :)
the -1 version is only for the initial trigger when the script gets loaded. the onclick handlers give the version with the number linked to the button. every button has their own value that they pass trough (their position in the list). if you want to increase it every time a button gets clicked you have to make a seperate counter and add to it everytime the function gets called
also: within CurrentScene(-1) the -1 part is just the parameter that gets passed to the CurrentScene function, not the function itself. when you call Invoke it just waits X seconds before executing wouthout a value.
Answer by upasnavig90 · Mar 22, 2018 at 11:30 AM
I guess u need to send the butonNumber parameter on buttonClick, on the basis of which switch case will work.