- Home /
The problem was simple and I found out for myself how to fix it
How to activate a panel by a toggle using a script?
I am creating a project about artistic gymnastics,it is a text-based game,it doesn't have any 3d models or anything. I created a panel that contains toggles,these toggles tell if the aparatus will be used or not(for now just the vaulting table toggle works). If the toggle is activated the "VaultPanel" is activated when the next button is clicked,but if is not the panel should not be activated when you click on the next button,but for some reason,the opposite is happening: using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ToggleSelection : MonoBehaviour {
public Toggle VaultToggle;
public Button NextButton;
public GameObject VaultPanel;
public GameObject UnevenBarsPanel;
public GameObject AparatusSelectionPanel;
void Start()
{
NextButton.onClick.AddListener(isVaulting);
}
void isVaulting()
{
if(VaultToggle == true)
{
AparatusSelectionPanel.gameObject.SetActive(false);
VaultPanel.gameObject.SetActive(true);
}
if(VaultToggle == false)
{
AparatusSelectionPanel.gameObject.SetActive(false);
VaultPanel.gameObject.SetActive(false);
UnevenBarsPanel.gameObject.SetActive(true);
}
}
}
(The uneven bars panel should be activated if the vaultToggle is false,but the opposite is happening)
Answer by jmailloux11 · Jul 30, 2020 at 08:21 PM
VaultToggle.gameObject.SetActive(!VaultToggle.gameObject.activeSelf); UnevenBarsPanel.gameObject.SetActive(!VaultToggle.gameObject.activeSelf);
Sould do the trick.
Just replace the if statements to be this part of code.
Also is the AparatusSelectionPanel supposed to be set false at all time?
you wrote "AparatusSelectionPanel.gameObject.SetActive(false);" twice!
The aparatus selection panel should be deactivated in both cases beacause when I press the next button the panel should close and open the next one than can be the vault panel or the uneven bars panel depending if the vaulttoggle is true or false
It didn't work,if the toggle is activated the uneven bars panel is called and if it's not the uneven bars panel still is called
@jmailloux11 Actually what do I do? Do I put this code in the condition or the condition action