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
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");
 
         // This GUILayoutUtility is useful because it is to fit the content
         Rect buttonGUI = GUILayoutUtility.GetRect(buttonText, "Button");
 
         // This 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 this?

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 things. 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 everything about delegates (Actions, Funcs, Predicates, Anonymous methods, lamda expression, etc) and how they work, Jamie King is the best source.

  • See this 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

Thank you so much for the answer (long answer :p) but it is very helpful for me vexe :D I appreciate that and will look at about your delegate stuff right now. Anyway, the script that has been posted in the question above is being simplified from what the my original script does (for the IInventory DoGUI function), I will look at where I have wrongly cause the DoGUI function is not executing after I look at your delegate stuff. Thank you so much once again :D

avatar image vexe · May 11, 2014 at 12:42 PM 0
Share

You do have both your scripts attached to gameObjects right? - Did you debug? if not, do so and do manual stepping. You could print the targets/methods of you delegate to see if the method is hooked or not:

 foreach(var d in myDel.GetInvocationList())
    Debug.Log("Target: " + d.Target + " $$anonymous$$ethod: " + d.$$anonymous$$ethod.Name);
avatar image Fuhans Puji Saputra · May 11, 2014 at 05:11 PM 0
Share

Sorry for the late reply. Yes, I already tried to debug it and the method of the DoGUI is fired / called. But, the problem is the the Inventory inside DoGUI function is not show, even though the variable that held the Inventory to be shown already true.

Do I need to post the original code in here sir?

I will share the link in here.

Thank you :D

avatar image vexe · May 11, 2014 at 05:39 PM 0
Share

I already tried to debug it and the method of the DoGUI is fired / called. But, the problem is the the Inventory inside DoGUI function is not show

That means it's a problem wasn't with delegates to begin with. But with your GUI code. So you're saying, you're expecting your inventory to show up but it's not? How are you drawing it? Currently, all you have is just a GUI.Box

avatar image Fuhans Puji Saputra · May 11, 2014 at 05:57 PM 0
Share

Here is the link sir:

link text

link text

Thank you for your help :D

I tried to delete all the code on the DoGUI method and replace it with GUI.Box only, but still no luck sir (The GUI.Box is not appearing on the screen)

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

Yes he does. But if you're talking about this line:

 IItemDatabase.onClicked += DoGUI;

Then no, he shouldn't be adding parentheses (not brackets btw ;) because he's not invoking the method he's just adding it to the delegate.

avatar image meat5000 ♦ · May 11, 2014 at 01:41 PM 1
Share

Cheers dude!

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

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

Sharing delegate types across scripts 1 Answer

Multiple Cars not working 1 Answer

Reloading Scene causes "null" object references. 3 Answers

Event to change level 1 Answer

Login form: Do I have to set custom delegates to null in OnDestroy? 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