I have this script in 3 different buttons in the UI. I need to make sure that just one panel is open when pressing a button. I use another script to receive the OnClickedPanel event and it subscribe to the event in Start(). When debugging the code all of the instances except one have OnClickedPanel = null.
public class PanelControl : MonoBehaviour
{
[SerializeField] GameObject panel;
public static PanelControl panelControl;
public event Action<string> OnClickedPanel;
private void OnEnable()
{
panelControl = this;
MenuControl.menuControl.OnMakeRoom += MakeRoom;
}
// Update is called once per frame
public void TogglePanel()
{
panel.SetActive(!panel.active);
Debug.Log(OnClickedPanel);
OnClickedPanel?.Invoke(this.name);
}
private void MakeRoom(string _openPanel)
{
if (_openPanel != this.name)
{
Debug.Log("opening" + _openPanel);
panel.SetActive(false);
}
}
private void OnDestroy()
{
MenuControl.menuControl.OnMakeRoom -= MakeRoom;
}
}
What am I messing up?