So I’ve been following the Modal Panel Tutorial, tweaking it a bit to work for me. But it hasnt been going great. I’ve been getting the error:
Here’s my code:
For ModalPanel.cs
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections;
public class ModalPanel : MonoBehaviour {
public Text question;
public Button yesButton;
public Button noButton;
public Button cancelButton;
public Button okButton;
public GameObject modalPanelObject;
private static ModalPanel modalPanel;
public static ModalPanel Instance () {
if (!modalPanel) {
modalPanel = FindObjectOfType(typeof (ModalPanel)) as ModalPanel;
if (!modalPanel)
Debug.LogError ("There needs to be one active ModalPanel script on a GameObject in your scene.");
}
return modalPanel;
}
//Yes/No/Cancel/Ok: A string, A Yes event, a no event, an OK event and a cancel event
public void Choice (string question, UnityAction yesEvent, UnityAction noEvent, UnityAction cancelEvent, UnityAction okEvent) {
modalPanelObject.SetActive (true);
yesButton.onClick.RemoveAllListeners ();
yesButton.onClick.AddListener (yesEvent);
yesButton.onClick.AddListener (ClosePanel);
noButton.onClick.RemoveAllListeners ();
noButton.onClick.AddListener (noEvent);
noButton.onClick.AddListener (ClosePanel);
cancelButton.onClick.RemoveAllListeners ();
cancelButton.onClick.AddListener (cancelEvent);
cancelButton.onClick.AddListener (ClosePanel);
okButton.onClick.RemoveAllListeners ();
okButton.onClick.AddListener (okEvent);
Application.LoadLevel("MainScene");
okButton.onClick.AddListener (ClosePanel);
this.question.text = question;
yesButton.gameObject.SetActive (false);
noButton.gameObject.SetActive (false);
cancelButton.gameObject.SetActive (false);
okButton.gameObject.SetActive (true);
}
void ClosePanel() {
modalPanelObject.SetActive (false);
}
}
For TestModalWindow.cs:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections;
public class TestModalWindow : MonoBehaviour {
private ModalPanel modalPanel;
private UnityAction myOkAction;
void Awake () {
modalPanel = ModalPanel.Instance ();
myOkAction = new UnityAction (TestOkFunction);
}
// Send to the Modal Panel to set up the Buttons and Functions to call
public void TestYNC () {
modalPanel.Choice ("Store Unavailable:The Store is currently unavailable.", myOkAction);
}
// These are wrapped into UnityActions
void TestOkFunction () {
Application.LoadLevel("MainScene");
}
}