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;
}
}
Found the error. Not sure why it caused the behavior it did, but it turns out that I accidentally put the child panel into the inspector and not the main panel. When I corrected that, this particular error disappeared.