I have a panel that gets activated by an onClick function, and then deactivated with a button. The activation side works perfectly well, but not so well with the deactivation/reactivation. The panel is set in the inspector.
If I use panel.setActive (true) in the activation script, the panel will activate and deactivate fine the first time but if I click the sprite again, it will activate the ‘selected’ boolean and change the spriterenderer but won’t activate the panel again.
If I use panel.setActive(!panel.activeSelf) in the activation script, the initial activation works but sometimes on deactivation, the panel won’t go away (and sometimes it does) and on other occasions it will go away but when I click the sprite again, it sometimes brings up the panel again, sometimes brings up only the first layer of the panel (ie. none of the child objects), and sometimes only activates the boolean and spriterenderer.
Activation script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Text;
public class CountrySelection : MonoBehaviour {
public static CountrySelection countryscript;
public GameObject panel;
public static GameObject currentlyselected;
public bool selected;
void OnMouseDown() {
selected = true;
transform.SetAsLastSibling ();
GetComponent<SpriteRenderer> ().color = Color.red;
currentlyselected = gameObject;
print ("Chosen" + currentlyselected.name);
panel.SetActive (true);
Deactivation script
using UnityEngine;
using System.Collections;
public class TogglePanelButtonscript : MonoBehaviour {
public void TogglePanel (GameObject panel) {
panel.SetActive (!panel.activeSelf);
CountrySelection.currentlyselected.GetComponent<CountrySelection> ().selected = false;
CountrySelection.currentlyselected.GetComponent<SpriteRenderer> ().color = Color.white;
}
}