How to enable/disable an Icon depending on the bool state of a toggle.
Hey guys so I have been trying to enable and disable icons in an action bar depending on whether or not a toggle is turned on in the previous scene. My approach: I have a lobby menu that has 12 abilities to choose from. You can choose up to 4 of these abilities. Each ability slot has a toggle component attached to it. I have a script that Is called GlobalDataManager this script is used between both the lobby menu scene and the the actual world scene. Inside GlobalDataManager I set up an array for gameobjects of each toggle and then declare 12 different toggle ID's as bools. I get the state of each toggle and if it is on then the matching toggle ID is set to true. I have a count down timer that when reaches 1 second should assign the toggles ID's and then after it reaches 0 seconds it loads the next scene. Inside the next scene I have a script Called UI Manager that checks the bool state of each toggle ID and then activates the ability icon dependent on the bool state. my problem is that when the world scene loads all the icons are turned on instead of the ones that were selected in the previous scene. Can someone explained to me what I am doing wrong.
Here is My Code. GlobalDataManager: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class GlobalDataManager : MonoBehaviour
{
public static GlobalDataManager Instance;
private float timer = 60f;
public GameObject[] toggleID;
public bool toggleID1 = false;
public bool toggleID2 = false;
public bool toggleID3 = false;
public bool toggleID4 = false;
public bool toggleID5 = false;
public bool toggleID6 = false;
public bool toggleID7 = false;
public bool toggleID8 = false;
public bool toggleID9 = false;
public bool toggleID10 = false;
public bool toggleID11 = false;
public bool toggleID12 = false;
void Awake()
{
if (Instance == null)
{
DontDestroyOnLoad(gameObject);
Instance = this;
}
else if (Instance != this)
{
Destroy (gameObject);
}
}
void Update()
{
timer -= Time.deltaTime;
if (timer == 5)
{
AssignToggles();
}
}
void AssignToggles()
{
if (toggleID[0].GetComponent<Toggle>().isOn = true)
{
toggleID1 = true;
}
if (toggleID[0].GetComponent<Toggle>().isOn = false)
{
toggleID1 = false;
}
if (toggleID[1].GetComponent<Toggle>().isOn = true)
{
toggleID2 = true;
}
if (toggleID[1].GetComponent<Toggle>().isOn = false)
{
toggleID2 = false;
}
if (toggleID[2].GetComponent<Toggle>().isOn = true)
{
toggleID3 = true;
}
if (toggleID[2].GetComponent<Toggle>().isOn = false)
{
toggleID3 = false;
}
if (toggleID[3].GetComponent<Toggle>().isOn = true)
{
toggleID4 = true;
}
if (toggleID[3].GetComponent<Toggle>().isOn = false)
{
toggleID4 = false;
}
if (toggleID[4].GetComponent<Toggle>().isOn = true)
{
toggleID5 = true;
}
if (toggleID[4].GetComponent<Toggle>().isOn = false)
{
toggleID5 = false;
}
if (toggleID[5].GetComponent<Toggle>().isOn = true)
{
toggleID6 = true;
}
if (toggleID[5].GetComponent<Toggle>().isOn = false)
{
toggleID6 = false;
}
if (toggleID[6].GetComponent<Toggle>().isOn = true)
{
toggleID7 = true;
}
if (toggleID[6].GetComponent<Toggle>().isOn = false)
{
toggleID7 = false;
}
if (toggleID[7].GetComponent<Toggle>().isOn = true)
{
toggleID8 = true;
}
if (toggleID[7].GetComponent<Toggle>().isOn = false)
{
toggleID8 = false;
}
if (toggleID[8].GetComponent<Toggle>().isOn = true)
{
toggleID9 = true;
}
if (toggleID[8].GetComponent<Toggle>().isOn = false)
{
toggleID9 = false;
}
if (toggleID[9].GetComponent<Toggle>().isOn = true)
{
toggleID10 = true;
}
if (toggleID[9].GetComponent<Toggle>().isOn = false)
{
toggleID10 = false;
}
if (toggleID[10].GetComponent<Toggle>().isOn = true)
{
toggleID11 = true;
}
if (toggleID[10].GetComponent<Toggle>().isOn = false)
{
toggleID11 = false;
}
if (toggleID[11].GetComponent<Toggle>().isOn = true)
{
toggleID12 = true;
}
if (toggleID[11].GetComponent<Toggle>().isOn = false)
{
toggleID12 = false;
}
}
}
UI Manager: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class UIManager : MonoBehaviour
{
public GameObject globalDataManager;
public GameObject[] abillityIcon;
// Start is called before the first frame update
void Start()
{
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID1 = true)
{
abillityIcon[0].SetActive(true);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID1 = false)
{
abillityIcon[0].SetActive(false);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID2 = true)
{
abillityIcon[1].SetActive(true);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID2 = false)
{
abillityIcon[1].SetActive(false);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID3 = true)
{
abillityIcon[2].SetActive(true);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID3 = false)
{
abillityIcon[2].SetActive(false);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID4 = true)
{
abillityIcon[3].SetActive(true);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID4 = false)
{
abillityIcon[3].SetActive(false);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID5 = true)
{
abillityIcon[4].SetActive(true);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID5 = false)
{
abillityIcon[4].SetActive(false);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID6 = true)
{
abillityIcon[5].SetActive(true);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID6 = false)
{
abillityIcon[5].SetActive(false);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID7 = true)
{
abillityIcon[6].SetActive(true);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID7 = false)
{
abillityIcon[6].SetActive(false);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID8 = true)
{
abillityIcon[7].SetActive(true);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID8 = false)
{
abillityIcon[7].SetActive(false);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID9 = true)
{
abillityIcon[8].SetActive(true);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID9 = false)
{
abillityIcon[8].SetActive(false);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID10 = true)
{
abillityIcon[9].SetActive(true);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID10 = false)
{
abillityIcon[9].SetActive(false);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID11 = true)
{
abillityIcon[10].SetActive(true);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID11 = false)
{
abillityIcon[10].SetActive(false);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID12 = true)
{
abillityIcon[11].SetActive(true);
}
if (globalDataManager.GetComponent<GlobalDataManager>().toggleID12 = false)
{
abillityIcon[11].SetActive(false);
}
}
}
What it looks like on play through. Lobby Menu:
World Scene:
Your answer
Follow this Question
Related Questions
Toggle Gravity on button press 1 Answer
How to change the Bool for only a single prefab GameObject creating with Instantiate? 2 Answers
Toggle a bool on only one object at a time by tapping an object. 0 Answers
If statement not working 1 Answer
how do i get a boolean to switch to true only after i've pressed a key three times? 2 Answers