{"id":8,"date":"2016-09-16T18:00:00","date_gmt":"2016-09-16T22:00:00","guid":{"rendered":""},"modified":"2018-12-18T09:07:43","modified_gmt":"2018-12-18T14:07:43","slug":"quick-tip-using-unity-actions-to-set-default-states-on-objects","status":"publish","type":"post","link":"https:\/\/naplandgames.com\/blog\/2016\/09\/16\/quick-tip-using-unity-actions-to-set-default-states-on-objects\/","title":{"rendered":"Quick tip: Using Unity Actions to set default states on objects"},"content":{"rendered":"<p><b><img loading=\"lazy\" decoding=\"async\" class=\"size-thumbnail wp-image-60 alignright\" src=\"https:\/\/naplandgames.com\/blog\/wp-content\/uploads\/2016\/09\/lightbulb-icon-150x150.png\" alt=\"\" width=\"150\" height=\"150\" srcset=\"https:\/\/naplandgames.com\/blog\/wp-content\/uploads\/2016\/09\/lightbulb-icon-150x150.png 150w, https:\/\/naplandgames.com\/blog\/wp-content\/uploads\/2016\/09\/lightbulb-icon-300x300.png 300w, https:\/\/naplandgames.com\/blog\/wp-content\/uploads\/2016\/09\/lightbulb-icon.png 625w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/><\/b>So as I was working on a project that has a bunch of UI panels I kept forgetting to disable certain panels before running the game or before committing it and my client would see the panels with the loading screen. Sure I could add a line to the controllers I have set up to set the default state, but there&#8217;s a bunch of controllers, not all of them have the panel assigned to them at start, not all of them are disabled (some are alpha 0, some are scale 0), and it&#8217;s really best when an object maintains its own default state. So I said to myself, &#8220;I really need a universal script to do this. Wouldn&#8217;t it be great to have some sort of drag-drop interface to do this?&#8221; I didn&#8217;t really want to write a custom inspector for it since that would take 2 scripts. Then it occurred to me: BUTTONS they have on-click events. That&#8217;s what I want. And boy was it easy!<\/p>\n<p>So all you have to do is create a <span style=\"font-family: 'courier new' , 'courier' , monospace;\">MonoBehaviour<\/span> derived class. Declare <span style=\"font-family: 'courier new' , 'courier' , monospace;\">using UnityEngine.Events<\/span>, declare a public <span style=\"font-family: 'courier new' , 'courier' , monospace;\">UnityAction<\/span> variable. Then in your <span style=\"font-family: 'courier new' , 'courier' , monospace;\">Start()<\/span> method just <span style=\"font-family: 'courier new' , 'courier' , monospace;\">Invoke()<\/span> that event. Super simple. Here&#8217;s the code:<br \/>\n<b><br \/>\n<\/b><b><\/b><\/p>\n<pre class=\"lang:c# decode:true \">using UnityEngine;\r\nusing UnityEngine.Events;\r\n \r\npublic class SetValueOnStart : MonoBehaviour\r\n{\r\n    public UnityEvent onStart;\r\n    void Start()\r\n    {\r\n        if (onStart != null)\r\n            onStart.Invoke();\r\n    }\r\n}<\/pre>\n<p>Way too easy. Of course, you could extend this to also do Awake, OnEnable, OnLevelWasLoaded, OnApplicationPause, OnBecameVisible\/Invisible, OnApplicationQuit, and OnDisable (I&#8217;d highly recommend avoiding using any of the main loop events). A very neat and clean way to do a simple delegate that&#8217;s assignable from the inspector and can set the value on pretty much any component.<\/p>\n<p>I hope you all find this as handy as I have. Feel free to post a more extended version here if you like sharing with others.<\/p>\n<p>As always, thanks for reading!<\/p>\n<p>On a personal note, my wife just published her first book! If you like kick-ass female\u00a0protagonists, spies, and a bit of sci-fi then please <a href=\"https:\/\/www.amazon.com\/dp\/B01LWDCRQP\">check out her book on Amazon<\/a>.<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_8\" class=\"pvc_stats all  \" data-element-id=\"8\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/naplandgames.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>So as I was working on a project that has a bunch of UI panels I kept forgetting to disable certain panels before running the game or before committing it and my client would see the panels with the loading screen. Sure I could add a line to the controllers I have set up to set the default state, but there&#8217;s a bunch of controllers, not all of them have the panel assigned to them at start, not all of them are disabled (some are alpha 0, some are scale 0), and it&#8217;s really best when an object maintains its&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/naplandgames.com\/blog\/2016\/09\/16\/quick-tip-using-unity-actions-to-set-default-states-on-objects\/\">Continue reading<span class=\"screen-reader-text\">Quick tip: Using Unity Actions to set default states on objects<\/span><\/a><\/div>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_8\" class=\"pvc_stats all  \" data-element-id=\"8\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/naplandgames.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":60,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"a3_pvc":{"activated":true,"total_views":11481,"today_views":0},"_links":{"self":[{"href":"https:\/\/naplandgames.com\/blog\/wp-json\/wp\/v2\/posts\/8"}],"collection":[{"href":"https:\/\/naplandgames.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/naplandgames.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/naplandgames.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/naplandgames.com\/blog\/wp-json\/wp\/v2\/comments?post=8"}],"version-history":[{"count":4,"href":"https:\/\/naplandgames.com\/blog\/wp-json\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":62,"href":"https:\/\/naplandgames.com\/blog\/wp-json\/wp\/v2\/posts\/8\/revisions\/62"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/naplandgames.com\/blog\/wp-json\/wp\/v2\/media\/60"}],"wp:attachment":[{"href":"https:\/\/naplandgames.com\/blog\/wp-json\/wp\/v2\/media?parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/naplandgames.com\/blog\/wp-json\/wp\/v2\/categories?post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/naplandgames.com\/blog\/wp-json\/wp\/v2\/tags?post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}