• 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
1
Question by Fuhans Puji Saputra · May 11, 2014 at 11:35 AM · c#eventdelegate

Delegate an event

Hi everyone, I am new at Unity and want to learn the delegate event, but I have a problem. I want when the button has been clicked, it will show some GUI button on it. I already tried the delegate, but the event is not fired (I mean the GUI button is not shown).

Here is the code that I am using:

The code below is to show the GUI button when I run the game.

 public class IItemDatabase : MonoBehaviour
 {
     public delegate void Action(); // Set the event for the button
 
     public static event Action onClicked; // The event for when the button has been clicked
 
     protected virtual void OnGUI()
     {
         // Call the SetStyle method
         SetStyle();
 
         // Set the GUIContent as the tooltip
         GUIContent buttonText = new GUIContent("Open Shop");
 
         // Set the GUIContent as the tooltip
         GUIContent buttonTexts = new GUIContent("Open Inventory");
 
         // T$$anonymous$$s GUILayoutUtility is useful because it is to fit the content
         Rect buttonGUI = GUILayoutUtility.GetRect(buttonText, "Button");
 
         // T$$anonymous$$s GUILayoutUtility is useful because it is to fit the content
         Rect buttonGUIs = GUILayoutUtility.GetRect(buttonTexts, "Button");
 
         // Set where have to the Rect displayed
         buttonGUI.x = 5;
         buttonGUI.y = Screen.height - 25;
 
         // Set where have to the Rect displayed
         buttonGUIs.x = 125;
         buttonGUIs.y = Screen.height - 25;
 
         // If the button has been clicked
 
         if (GUI.Button(buttonGUI, buttonText, style))
         {
             if (onClicked != null)
             {
                 onClicked();
             }
         }
 
         if (GUI.Button(buttonGUIs, buttonTexts, style))
         {
             if (onClicked != null)
             {
                 onClicked();
             }
         }
 
         // End of the clicked button event
     }
 }

And here is the I want it to display when the button has been clicked:

 public class IInventory : MonoBehaviour 
 {
 
     protected virtual void OnEnable()
     {
         IItemDatabase.onClicked += DoGUI;
     }
 
     protected virtual void OnDisable()
     {
         IItemDatabase.onClicked -= DoGUI;
     }
 
     protected virtual void DoGUI()
     {
        Rect slotRect = new Rect(x * 35 + (Screen.width / 3) + 50, y * 35 + (Screen.height / 3) - 10, 30, 30);
                 GUI.Box(slotRect, GUIContent.none);
     }
 }

But when I clicked the button that it suppose to fired the DoGUI() in IInventory class, it does not run the function.

How do I solve t$$anonymous$$s?

Thank you.

Your answer much appreciated!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by vexe · May 11, 2014 at 12:19 PM

I'm always happy to see questions about delegates :)

You seem to have the idea that a delegate and event are different t$$anonymous$$ngs. An event is a delegate - only difference event has is there is an added layer of abstraction and security that makes it impossible to set the delegate value directly, instead you're only allowed to add/remove methods. In other words, with delegates it is possible to do: do myDel = value; and myDel X= value; (where X: - or +) But with events only myEvent X= value; is allowed (where X: - or +) A lot of times, the terms "event" and "delegate" are used interchangeably.

public delegate void Action(); // Set the event for the button

There's already a built-in delegate called Action for you in that exact signature (in the System namespace)

I ran your scripts and they worked fine. DoGUI did get executed.

I like what you're doing - You're following the single responsibility principle by indirectly having your buttons reference the inventory by using delegates - because it's not the button's responsibility to determine what happens in an inventory +1 However; I'm not very fond of the delegate being static. I suggest the following change of design: Create a Slot class that has an Item (if you wish) - The inventory will then have an aggregate (list) of Slots - Each Slot has an onClick - When the inventory creates a slot for ex, it subscribes itself to the slot's onClick so that when a slot/button is clicked, it indirectly notifies the inventory. No need for statics.

More delegate stuff:

  • If you want to learn everyt$$anonymous$$ng about delegates (Actions, Funcs, Predicates, Anonymous methods, lamda expression, etc) and how they work, Jamie King is the best source.

  • See t$$anonymous$$s answer here on how to make delegates survive an assembly reload.

  • For a full solution on serializable/inspectable delegate, see my uFAction.

Comment
Add comment · Show 7 · 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 Fuhans Puji Saputra · May 11, 2014 at 12:33 PM 0
Share
avatar image vexe · May 11, 2014 at 12:42 PM 0
Share
avatar image Fuhans Puji Saputra · May 11, 2014 at 05:11 PM 0
Share
avatar image vexe · May 11, 2014 at 05:39 PM 0
Share
avatar image Fuhans Puji Saputra · May 11, 2014 at 05:57 PM 0
Share
Show more comments
avatar image
0

Answer by meat5000 · May 11, 2014 at 11:36 AM

I don't do C# but don't you need to add the brackets to call a function?

DoGUI();

Comment
Add comment · 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 vexe · May 11, 2014 at 01:01 PM 1
Share
avatar image meat5000 ♦ · May 11, 2014 at 01:41 PM 1
Share

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

22 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 avatar image

Related Questions

Parametrize generic event system (DRY it) 1 Answer

C# event setup, no result 0 Answers

Sharing delegate types across scripts 1 Answer

Multiple Cars not working 1 Answer

Reloading Scene causes "null" object references. 3 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