{"id":39,"date":"2013-03-19T21:07:35","date_gmt":"2013-03-19T21:07:35","guid":{"rendered":"http:\/\/lotecsoftware.com\/unity\/blog\/?page_id=39"},"modified":"2013-03-19T21:07:35","modified_gmt":"2013-03-19T21:07:35","slug":"switchtrigger","status":"publish","type":"page","link":"http:\/\/lotecsoftware.com\/unity\/blog\/?page_id=39","title":{"rendered":"SwitchTrigger"},"content":{"rendered":"<pre class=\"lang:c# decode:true\" title=\"SwitchTrigger\">\/**\r\n * Add this script to a GameObject with a trigger collider.\r\n * Sends information to the trigger target about the trigger status.\r\n * Also plays on\/off animation for trigger if available.\r\n **\/\r\nusing UnityEngine;\r\nusing System.Collections;\r\n\r\npublic class SwitchTrigger : MonoBehaviour {\r\n\tpublic SwitchTriggerTarget triggerObject;\r\n\tpublic AnimationClip onClip;\r\n\tpublic AnimationClip offClip;\r\n\tprivate int cnt;\r\n\r\n\tvoid Awake() {\r\n\t\tcnt = 0;\r\n\t\tif (onClip)\r\n\t\t\tGetComponent&lt;Animation&gt;().AddClip(onClip, \"on\");\r\n\t\tif (offClip)\r\n\t\t\tGetComponent&lt;Animation&gt;().AddClip(offClip, \"off\");\r\n\t}\r\n\r\n\tvoid OnTriggerEnter(Collider other) {\r\n\t\tcnt++;\r\n\t\tif (cnt == 1) {\r\n\t\t\t\/\/ Send trigger message to linked object.\r\n\t\t\tif (triggerObject) {\r\n\t\t\t\ttriggerObject.OnTriggerSwitch(true);\r\n\t\t\t} else {\r\n\t\t\t\tDebug.Log(\"No trigger object attached.\");\r\n\t\t\t}\r\n\r\n\t\t\t\/\/ Play on animation, if available.\r\n\t\t\tif (onClip)\r\n\t\t\t\tGetComponent&lt;Animation&gt;().Play(\"on\");\r\n\t\t}\r\n\t}\r\n\r\n\tvoid OnTriggerExit(Collider other) {\r\n\t\tcnt--;\r\n\t\tif (cnt == 0) {\r\n\t\t\t\/\/ Send trigger message to linked object.\r\n\t\t\tif (triggerObject)\r\n\t\t\t\ttriggerObject.OnTriggerSwitch(false);\r\n\r\n\t\t\t\/\/ Play off animation, if available.\r\n\t\t\tif (offClip)\r\n\t\t\t\tGetComponent&lt;Animation&gt;().Play(\"off\");\r\n\t\t}\r\n\t}\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/** * Add this script to a GameObject with a trigger collider. * Sends information to the trigger target about the trigger status. * Also plays on\/off animation for trigger if available. **\/ using UnityEngine; using System.Collections; public class SwitchTrigger : MonoBehaviour { public SwitchTriggerTarget triggerObject; public AnimationClip onClip; public AnimationClip offClip; private int cnt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=\/wp\/v2\/pages\/39"}],"collection":[{"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=39"}],"version-history":[{"count":1,"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=\/wp\/v2\/pages\/39\/revisions"}],"predecessor-version":[{"id":40,"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=\/wp\/v2\/pages\/39\/revisions\/40"}],"wp:attachment":[{"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}