- Home /
how to call a class on click
i want to create a dialog box with yes and no button.,so i used the gui.window and create it a script.,but now i want to call this script on back button click.
toast.cs
void OnGUI() {
if (GUI.Button (new Rect (10, 350, 70, 20), "Back "))
{
}
}
dialog.cs
public class dialog : MonoBehaviour {
public GUIStyle mystyle;
public Rect windowRect = new Rect (150, 80, 200, 100) ;
public void OnGUI () {
windowRect = GUI.Window (0, windowRect, WindowFunction, "Save !!!");
}
public void WindowFunction (int windowID) {
GUI.Label( new Rect( 40, 40, 120, 50 ), "Do you want to save ?? ",mystyle );
if (GUI.Button (new Rect (10, 70, 70, 20), "Yes "))
{
Application.LoadLevel("Settings");
}
if (GUI.Button (new Rect (120, 70, 70, 20), "No "))
{
Application.LoadLevel("Settings");
}
}
}
Answer by PouletFrit · May 28, 2014 at 02:12 PM
You can't call a class. A class is only a template for creating object. You define your variables and his behaviour (method/functions) for this group of object in a class.
You then need to instantiate this class to create an object.
In Unity, if your class inherit from MonoBehaviour (which is the default):
public class MyClass : MonoBehaviour {
}
you only have to attach it to a GameObject to instantiate it.
So once you have an instance of your object you can then call his function or his methods.
In your case you want to be able to display a window when you have clicked on a button. Attach both of your script on the same GameObject and modify a little bit dialog.cs to look like this:
public class dialog : MonoBehaviour {
public GUIStyle mystyle;
public Rect windowRect = new Rect (150, 80, 200, 100);
// Declare a boolean letting this object know when to display the window
private bool displayDialog;
public void OnGUI () {
// Only display the window when displayDialog is true
if (displayDialog) {
windowRect = GUI.Window (0, windowRect, WindowFunction, "Save !!!");
}
}
public void WindowFunction (int windowID) {
GUI.Label( new Rect( 40, 40, 120, 50 ), "Do you want to save ?? ",mystyle );
if (GUI.Button (new Rect (10, 70, 70, 20), "Yes "))
{
Application.LoadLevel("Settings");
}
if (GUI.Button (new Rect (120, 70, 70, 20), "No "))
{
Application.LoadLevel("Settings");
}
}
public void OpenDialog() {
displayDialog = true;
}
public void CloseDialog() {
displayDialog = false;
}
}
and then just add this in ur toast.cs:
// We declare a global variable so we can access it from anywhere in this class
public dialog dialogScript;
void Start() {
// Associate the dialog instance of this GameObject to the global variable
dialogScript = gameObject.GetComponent<dialog>();
}
void OnGUI() {
if (GUI.Button(new Rect (10, 350, 70, 20), "Back "))
{
// When the Back button is clicked, call the OpenWindow function on the dialog instance
dialogScript.OpenWindow();
}
}
Your answer
Follow this Question
Related Questions
Create reset button to main camera 1 Answer
How to create a UnityScript array and access the data in each cell. 1 Answer
How to handle 5 touches at a time? 2 Answers
Unity3d Update issue 2 Answers
Game/editor stops responding when going to Highscores 0 Answers