Newbie Newbie - 2 months ago 15
C# Question

How to start a script via trigger in unity

I am trying to activate a script via a trigger. I have a script that makes an object float and spin, but i would like to have that script start when the player gets close to the object. At the moment the object the floats when the game starts and not when the player activates the trigger.

Here is the floating script:

public class AutoMoveAndRotate : MonoBehaviour
{
public Vector3andSpace moveUnitsPerSecond;
public Vector3andSpace rotateDegreesPerSecond;
public bool ignoreTimescale;
private float m_LastRealTime;


private void Start()
{
m_LastRealTime = Time.realtimeSinceStartup;
}


// Update is called once per frame
private void Update()
{
float deltaTime = Time.deltaTime;
if (ignoreTimescale)
{
deltaTime = (Time.realtimeSinceStartup - m_LastRealTime);
m_LastRealTime = Time.realtimeSinceStartup;
}
transform.Translate(moveUnitsPerSecond.value*deltaTime, moveUnitsPerSecond.space);
transform.Rotate(rotateDegreesPerSecond.value*deltaTime, moveUnitsPerSecond.space);
}


[Serializable]
public class Vector3andSpace
{
public Vector3 value;
public Space space = Space.Self;
}
}
}


And here is the trigger script (attached to an object placed before the floating object) :

public class ActivateTrigger : MonoBehaviour
{
// A multi-purpose script which causes an action to occur when
// a trigger collider is entered.
public enum Mode
{
Trigger = 0, // Just broadcast the action on to the target
Replace = 1, // replace target with source
Activate = 2, // Activate the target GameObject
Enable = 3, // Enable a component
Animate = 4, // Start animation on target
Deactivate = 5 // Decativate target GameObject
}

public Mode action = Mode.Activate; // The action to accomplish
public Object target; // The game object to affect. If none, the trigger work on this game object
public GameObject source;
public int triggerCount = 1;
public bool repeatTrigger = false;


private void DoActivateTrigger()
{
triggerCount--;

if (triggerCount == 0 || repeatTrigger)
{
Object currentTarget = target ?? gameObject;
Behaviour targetBehaviour = currentTarget as Behaviour;
GameObject targetGameObject = currentTarget as GameObject;
if (targetBehaviour != null)
{
targetGameObject = targetBehaviour.gameObject;
}

switch (action)
{
case Mode.Trigger:
if (targetGameObject != null)
{
targetGameObject.BroadcastMessage("DoActivateTrigger");
}
break;
case Mode.Replace:
if (source != null)
{
if (targetGameObject != null)
{
Instantiate(source, targetGameObject.transform.position,
targetGameObject.transform.rotation);
DestroyObject(targetGameObject);
}
}
break;
case Mode.Activate:
if (targetGameObject != null)
{
targetGameObject.SetActive(true);
}
break;
case Mode.Enable:
if (targetBehaviour != null)
{
targetBehaviour.enabled = true;
}
break;
case Mode.Animate:
if (targetGameObject != null)
{
targetGameObject.GetComponent<Animation>().Play();
}
break;
case Mode.Deactivate:
if (targetGameObject != null)
{
targetGameObject.SetActive(false);
}
break;
}
}
}


private void OnTriggerEnter(Collider other)
{
DoActivateTrigger();
}
}
}


If i add the floating script to the object it start floating regardless if the player walked through the trigger. So not sure how to make this work?

Please help?

Many Thanks

Answer

The AutoMoveAndRotate component on the the floating object should be deactived before you starting the game. Otherwise it will execute once the game starts.