Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Himani_123 · May 28, 2014 at 01:25 PM · javascriptdialog

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");    
         }
         }
         
     }


 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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();
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Himani_123 · May 29, 2014 at 04:34 AM 0
Share

@ PouletFrit thank you :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges