1 using System;
2 using
UnityEngine;
3 using
Object = UnityEngine.Object;
4
5 namespace
UnityStandardAssets.Utility
6 {
7     
public class ActivateTrigger : MonoBehaviour
8     {
9         
// A multi-purpose script which causes an action to occur when
10         
// a trigger collider is entered.
11         
public enum Mode
12         {
13             Trigger =
0, // Just broadcast the action on to the target
14             Replace =
1, // replace target with source
15             Activate =
2, // Activate the target GameObject
16             Enable =
3, // Enable a component
17             Animate =
4, // Start animation on target
18             Deactivate =
5 // Decativate target GameObject
19         }
20
21         
public Mode action = Mode.Activate; // The action to accomplish
22         
public Object target; // The game object to affect. If none, the trigger work on this game object
23         
public GameObject source;
24         
public int triggerCount = 1;
25         
public bool repeatTrigger = false;
26
27
28         
private void DoActivateTrigger()
29         {
30             triggerCount--;
31
32             
if (triggerCount == 0 || repeatTrigger)
33             {
34                 Object currentTarget = target ?? gameObject;
35                 Behaviour targetBehaviour = currentTarget
as Behaviour;
36                 GameObject targetGameObject = currentTarget
as GameObject;
37                 
if (targetBehaviour != null)
38                 {
39                     targetGameObject = targetBehaviour.gameObject;
40                 }
41
42                 
switch (action)
43                 {
44                     
case Mode.Trigger:
45                         
if (targetGameObject != null)
46                         {
47                             targetGameObject.BroadcastMessage(
"DoActivateTrigger");
48                         }
49                         
break;
50                     
case Mode.Replace:
51                         
if (source != null)
52                         {
53                             
if (targetGameObject != null)
54                             {
55                                 Instantiate(source, targetGameObject.transform.position,
56                                             targetGameObject.transform.rotation);
57                                 DestroyObject(targetGameObject);
58                             }
59                         }
60                         
break;
61                     
case Mode.Activate:
62                         
if (targetGameObject != null)
63                         {
64                             targetGameObject.SetActive(
true);
65                         }
66                         
break;
67                     
case Mode.Enable:
68                         
if (targetBehaviour != null)
69                         {
70                             targetBehaviour.enabled =
true;
71                         }
72                         
break;
73                     
case Mode.Animate:
74                         
if (targetGameObject != null)
75                         {
76                             targetGameObject.GetComponent<Animation>().Play();
77                         }
78                         
break;
79                     
case Mode.Deactivate:
80                         
if (targetGameObject != null)
81                         {
82                             targetGameObject.SetActive(
false);
83                         }
84                         
break;
85                 }
86             }
87         }
88
89
90         
private void OnTriggerEnter(Collider other)
91         {
92             DoActivateTrigger();
93         }
94     }
95 }