{"id":33,"date":"2013-03-19T21:00:43","date_gmt":"2013-03-19T21:00:43","guid":{"rendered":"http:\/\/lotecsoftware.com\/unity\/blog\/?p=33"},"modified":"2022-09-10T15:27:00","modified_gmt":"2022-09-10T13:27:00","slug":"the-interface-problem","status":"publish","type":"post","link":"http:\/\/lotecsoftware.com\/unity\/blog\/?p=33","title":{"rendered":"The interface problem"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>So I wanted to use an interface reference as a public variable, but it wouldn&#8217;t show up in the inspector. After many hours researching and trying different things, I finally settled for using abstract classes instead, and using the unity way of adding scripts to objects as my interface.<\/p>\n<p>I made a pad with a button on it. This button is my switch. When something (player or box) moves onto it, it should trigger an event in another target object. So I wanted to have a SwitchTrigger interface for the target of my switch object.<\/p>\n<pre class=\"lang:c# decode:true\">class TargetObject : MonoBehaviour, ISwitchTrigger {\r\n...\r\n void OnSwitchTrigger(bool on) {\r\n  \/\/ This object has been turned on\/off.\r\n }\r\n}<\/pre>\n<p>What I did instead was the following:<\/p>\n<pre class=\"lang:c# decode:true\" title=\"SwitchTriggerTarget\">\/* This should have been an interface, but Unity don't\r\n * allow interface objects as public references in the gui.\r\n * *\/\r\nusing UnityEngine;\r\n\r\npublic abstract class SwitchTriggerTarget : MonoBehaviour {\r\n\tpublic abstract void OnTriggerSwitch(bool on);\r\n}<\/pre>\n<p>Then I have to create an explicit class inheriting this abstract class for the specific GameObject I want it on.<\/p>\n<pre class=\"lang:c# decode:true\" title=\"MovingPlatformTriggerTarget\">public class MovingPlatformTriggerTarget : SwitchTriggerTarget {\r\n\tpublic override void OnTriggerSwitch(bool on) {\r\n\t\tDebug.Log(\"Platform got trigger=\" + on);\r\n\t\tGetComponent&lt;SeekSteer&gt;().enabled = on;\r\n\t}\r\n}<\/pre>\n<p>I then add the script SwitchTriggerTarget to my MovingPlatform object.<\/p>\n<p>MovingPlatform object also has a MovingPlatform script, which basically just makes sure other objects on MovingPlatform object follows it when it moves. This is done by parenting.<\/p>\n<pre class=\"lang:c# decode:true\">public class MovingPlatform: MonoBehaviour {\r\n\t\/\/ TODO: Add objects on platform to an array.\r\n\t\/\/ Save old parent on enter, reattach old parent on exit.\r\n\r\n\tvoid OnTriggerEnter(Collider other) {\r\n\t\tother.transform.parent = gameObject.transform;\r\n\t}\r\n\r\n\tvoid OnTriggerExit(Collider other) {\r\n\t\tother.transform.parent = null;\r\n\t}\r\n}<\/pre>\n<p>The platform movement is done by another script. Just add the modules you want onto the GameObject, and then make a prefab of it. Modular and nice.<\/p>\n<p>Now, my SwitchPad (the one you walk on to trigger the button), looks like this.<\/p>\n<pre class=\"lang:c# decode:true mark:2\">public 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<p>The highlighted row: By using the abstract class as the type, only GameObjects with scripts inheriting from SwitchTarget, like MovablePlatformSwitchTarget, can be dragged onto the target in the inspector.<\/p>\n<p>This works, but everything would have been easier if an interface could be used as type and shown in inspector&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; So I wanted to use an interface reference as a public variable, but it wouldn&#8217;t show up in the inspector. After many hours researching and trying different things, I finally settled for using abstract classes instead, and using the unity way of adding scripts to objects as my interface. I made a pad with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[9],"_links":{"self":[{"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=\/wp\/v2\/posts\/33"}],"collection":[{"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"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=33"}],"version-history":[{"count":6,"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":88,"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=\/wp\/v2\/posts\/33\/revisions\/88"}],"wp:attachment":[{"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/lotecsoftware.com\/unity\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}