{"id":3560,"date":"2015-01-10T17:43:07","date_gmt":"2015-01-10T14:43:07","guid":{"rendered":"http:\/\/unitycoder.com\/blog\/?p=3560"},"modified":"2015-01-10T19:22:59","modified_gmt":"2015-01-10T16:22:59","slug":"simple-event-delegate-script","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2015\/01\/10\/simple-event-delegate-script\/","title":{"rendered":"Simple Event &#038; Delegate Script"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3561\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2015\/01\/10\/simple-event-delegate-script\/events_delegates\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2015\/01\/events_delegates.jpg?fit=680%2C400&amp;ssl=1\" data-orig-size=\"680,400\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"events_delegates\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2015\/01\/events_delegates.jpg?fit=680%2C400&amp;ssl=1\" class=\"alignnone size-full wp-image-3561\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2015\/01\/events_delegates.jpg?resize=680%2C400\" alt=\"events_delegates\" width=\"680\" height=\"400\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2015\/01\/events_delegates.jpg?w=680&amp;ssl=1 680w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2015\/01\/events_delegates.jpg?resize=300%2C176&amp;ssl=1 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n<p>Just a quick test on events &amp; delegates (taken from <a title=\"http:\/\/unity3d.com\/learn\/tutorials\/modules\/intermediate\/live-training-archive\/merry-fragmas-multiplayer-fps-2\" href=\"http:\/\/unity3d.com\/learn\/tutorials\/modules\/intermediate\/live-training-archive\/merry-fragmas-multiplayer-fps-2\" target=\"_blank\">this live tutorial)<\/a><br \/>\n&#8211; New scene<br \/>\n&#8211; Add Sphere object to the scene (and move it into Main Camera view, for example 0,0,0)<br \/>\n&#8211; Create new script: FireEvent.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing UnityEngine;\r\nusing System.Collections;\r\n\r\npublic class FireEvent : MonoBehaviour\r\n{\r\n\u00a0\u00a0 \u00a0public delegate void Clicked();\r\n\u00a0\u00a0 \u00a0public event Clicked WasClicked;\r\n\r\n\u00a0\u00a0 \u00a0void OnMouseDown()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (WasClicked!=null) \/\/ we have someone listening\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Debug.Log(&quot;We have a listener, send event&quot;);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0WasClicked();\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}else{\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Debug.Log(&quot;Nobody is listening!!!! noooooo&quot;);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&#8211; Attach &#8220;FireEvent.cs&#8221; script into Sphere gameobject<br \/>\n&#8211; Create new script: Listener.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing UnityEngine;\r\nusing System.Collections;\r\n\r\npublic class Listener : MonoBehaviour {\r\n\r\n\u00a0\u00a0 \u00a0public GameObject EventManager; \/\/ reference to event firing object\r\n\r\n\u00a0\u00a0 \u00a0void Start()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ subscribe to the event (attach listener)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0EventManager.GetComponent&lt;FireEvent&gt;().WasClicked+=SomethingWasClickedOutside; \/\/ call this function if event is fired\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0void SomethingWasClickedOutside()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Debug.Log (&quot;Listener.cs: Event was fired!&quot;);\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n<\/pre>\n<p>&#8211; Create new Empty Gameobject<br \/>\n&#8211; Attach &#8220;Listener.cs&#8221; to that empty gameobject<br \/>\n&#8211; Assign Sphere into the &#8220;EventManager&#8221; field in the inspector<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3562\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2015\/01\/10\/simple-event-delegate-script\/listener_events\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2015\/01\/listener_events.jpg?fit=595%2C263&amp;ssl=1\" data-orig-size=\"595,263\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"listener_events\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2015\/01\/listener_events.jpg?fit=595%2C263&amp;ssl=1\" class=\"alignnone size-full wp-image-3562\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2015\/01\/listener_events.jpg?resize=595%2C263\" alt=\"listener_events\" width=\"595\" height=\"263\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2015\/01\/listener_events.jpg?w=595&amp;ssl=1 595w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2015\/01\/listener_events.jpg?resize=300%2C133&amp;ssl=1 300w\" sizes=\"auto, (max-width: 595px) 100vw, 595px\" \/><br \/>\n&#8211; Hit play and then click the Sphere with mouse, event should be fired.<\/p>\n<p><strong>More info:<\/strong><br \/>\n<a title=\"http:\/\/unity3d.com\/learn\/tutorials\/modules\/intermediate\/scripting\/events\" href=\"http:\/\/unity3d.com\/learn\/tutorials\/modules\/intermediate\/scripting\/events\" target=\"_blank\">http:\/\/unity3d.com\/learn\/tutorials\/modules\/intermediate\/scripting\/events<\/a><br \/>\n<a title=\"http:\/\/unity3d.com\/learn\/tutorials\/modules\/intermediate\/scripting\/delegates\" href=\"http:\/\/unity3d.com\/learn\/tutorials\/modules\/intermediate\/scripting\/delegates\" target=\"_blank\">http:\/\/unity3d.com\/learn\/tutorials\/modules\/intermediate\/scripting\/delegates<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just a quick test on events &amp; delegates (taken from this live tutorial) &#8211; New scene &#8211; Add Sphere object to the scene (and move it into Main Camera view, for example 0,0,0) &#8211; Create new script: FireEvent.cs using UnityEngine; using System.Collections; public class FireEvent [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3561,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[420,3],"tags":[68,696,695,242,694,697,383],"class_list":["post-3560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","category-unity3d","tag-c","tag-delegate","tag-deletages","tag-event","tag-events","tag-scripting","tag-tutorial"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2015\/01\/events_delegates.jpg?fit=680%2C400&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-Vq","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/3560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=3560"}],"version-history":[{"count":4,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/3560\/revisions"}],"predecessor-version":[{"id":3567,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/3560\/revisions\/3567"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/3561"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=3560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=3560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=3560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}