• 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
Question by Seyren · Jun 08, 2015 at 10:51 AM · c#uimessagebox

Issues building an in-game MsgBox

Hi guys, i want to build a Message Box that i can use at any point, however i found some issues.

First here is how i do it:

 public class MsgBox : MonoBehaviour 
 {
     void Update () 
     {
     
     }
 
     public static void CreateMsgBox(string t, string b)
     {
         GameObject msgbox = Instantiate(Resources.Load("UI/Prefab/MessageBox")) as GameObject;
         msgbox.transform.SetParent((GameObject.Find("Canvas").transform), false);
 
         Text[] info = msgbox.GetComponentsInChildren<Text>();
         info[0].text = t;
         info[1].text = b;
 
     }
 }

And this is how i call it:

         MsgBox.CreateMsgBox("Since this is the first time entering the Offline Netsphere, you must create a user.","Confirm");


Here is the thing though, I want to modify what happens after the "Confirm"(For example) button is clicked, And get it closed when clicked too.

So i thought adding an OnClick() on the class but that didn't work obviously,

I guess i can solve the closing thing if add it to the prefab directly(didn't think about it), but that's not my main issue anyway.

How can i add an event after clicking the Button of the messagebox? I guess you could check in the Update Function if it gets destroyed, but seems kind of messy to me.

Any suggestions of how can i handle this? I would really appreciate it

Thanks in advance :)

Comment
sarthakshah

People who like this

1 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Seyren · Jun 09, 2015 at 09:49 PM

Thanks for all the help guys! this is what i figured out after your two suggestions!

I made the MsgBox able to return a Button, so this way:

         Button b = MsgBox.CreateMsgBox("Since this is the first time entering the Offline Netsphere, you must create a user.","Confirm");
         b.onClick.AddListener(CreateName);
 
 
     void CreateName()
     {
         Debug.Log("YOU ARE A SQUID NOOOOOOOOOOW");
     }
 

I can add any function from anywhere i want :D
I got inspired by both the suggestions and the video guide, thanks you both :P

Comment

People who like this

0 Show 0 · 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

Answer by Bioinformatizer · Jun 09, 2015 at 12:15 AM

One way to get a button to help in this context is to add a new public method to your script and reference this with a button that is already on the prefab.

Let's say that you have a button on the prefab already.

You then need a public reference on your script, something like :

 public void ThisButton(){
      Debug.Log("This button was clicked");
 }

Now you go to the inspector of the button on the prefab and at the bottom add and event with the '+' symbol and select onClick().

Put the prefab containing the script into the select object.

Lastly you select the script in the function select and pick your method ThisButton() and it will print your debug (or whatever else you would like within the block).

Good Luck with the buttons!

Comment
Seyren

People who like this

1 Show 2 · 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 Seyren · Jun 09, 2015 at 11:45 AM 0
Share

Would this mean that i have to add a prefab of the button everytime i want to do something different? just asking if i could do ThisButton() function outside the prefab to prevent this, like passing it as a parameter or something, since i want to use the Message box for a lot of things, this is how it looks like: http://puu.sh/iiba7/b6e74e1711.jpg thanks a lot on the rest though :P

avatar image Bioinformatizer · Jun 09, 2015 at 12:13 PM 0
Share

As long as you have the link established between that button and the code, you should be able to handle a wide variety of conditions. A way to do this is using the Switch method.

You may have to pass an additional parameter to the msgBox call you used above though.

Try and give the CreateMessageBox another parameter int boxCase. You would need to declare the int buttonCase above Update as normal and then set 'buttonCase = boxCase'. Lastly you could use the buttonCase for a Swtich like this :

  public void ThisButton()
     {
         switch (buttonCase)
         {
         case 5:
             StartLevelFuntion();
             break;
         case 4:
             OpenNewMessageBox();
             break;
         case 3:
             int seven = 5;
             int eleven = 5;
             Debug.Log(seven+eleven);
             break;
         case 2:
             print ("message complete");
             break;
         case 1:
             InstantiateClownBehindYou();
             break;
         default:
             print ("MessageCase is not set right");
             break;
         }
     }
 }

avatar image

Answer by xSmurfKasketter · Jun 09, 2015 at 12:18 PM

You could use events, Unity live training have a great lesson to make a modal UI system, more or less exactly what you would like and like this, you save some overhead and can use it everywhere, this is the link: http://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/modal-window

You can ofcourse add the method to the button directly like Bioinformatizer suggested, however this couples it quite tightly with the script instead of having something you can use everywhere

Comment
Seyren

People who like this

1 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

[4.6] Toggle/Checkbox On/Off 1 Answer

How would I add a scene delay timer to my script? 1 Answer

How to create a popup after a specific level 1 Answer


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