So I wanted to use an interface reference as a public variable, but it wouldn’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 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.

C#

1

2

3

4

5

6

classTargetObject:MonoBehaviour,ISwitchTrigger{

...

voidOnSwitchTrigger(boolon){

// This object has been turned on/off.

}

}

What I did instead was the following:

SwitchTriggerTarget

C#

1

2

3

4

5

6

7

8

/* This should have been an interface, but Unity don't

* allow interface objects as public references in the gui.

* */

usingUnityEngine;

publicabstractclassSwitchTriggerTarget:MonoBehaviour{

publicabstractvoidOnTriggerSwitch(boolon);

}

Then I have to create an explicit class inheriting this abstract class for the specific GameObject I want it on.

MovingPlatformTriggerTarget

C#

1

2

3

4

5

6

publicclassMovingPlatformTriggerTarget:SwitchTriggerTarget{

publicoverridevoidOnTriggerSwitch(boolon){

Debug.Log("Platform got trigger="+on);

GetComponent<SeekSteer>().enabled=on;

}

}

I then add the script SwitchTriggerTarget to my MovingPlatform object.

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.

C#

1

2

3

4

5

6

7

8

9

10

11

12

publicclassMovingPlatform:MonoBehaviour{

// TODO: Add objects on platform to an array.

// Save old parent on enter, reattach old parent on exit.

voidOnTriggerEnter(Collider other){

other.transform.parent=gameObject.transform;

}

voidOnTriggerExit(Collider other){

other.transform.parent=null;

}

}

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.

Now, my SwitchPad (the one you walk on to trigger the button), looks like this.

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

publicclassSwitchTrigger:MonoBehaviour{

publicSwitchTriggerTarget triggerObject;

publicAnimationClip onClip;

publicAnimationClip offClip;

privateintcnt;

voidAwake(){

cnt=0;

if(onClip)

GetComponent<Animation>().AddClip(onClip,"on");

if(offClip)

GetComponent<Animation>().AddClip(offClip,"off");

}

voidOnTriggerEnter(Collider other){

cnt++;

if(cnt==1){

// Send trigger message to linked object.

if(triggerObject){

triggerObject.OnTriggerSwitch(true);

}else{

Debug.Log("No trigger object attached.");

}

// Play on animation, if available.

if(onClip)

GetComponent<Animation>().Play("on");

}

}

voidOnTriggerExit(Collider other){

cnt--;

if(cnt==0){

// Send trigger message to linked object.

if(triggerObject)

triggerObject.OnTriggerSwitch(false);

// Play off animation, if available.

if(offClip)

GetComponent<Animation>().Play("off");

}

}

}

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.

This works, but everything would have been easier if an interface could be used as type and shown in inspector…