- Home /
Help with Trigger script
Hi guys, help needed. I tried to make a script that will stop my character when trigger is activated. For some reason this doesnt work so I need your help. Script is added to object callled "Trap".
public var enableTrigger : boolean;
public var t = 0.0;
public var t1 = 0.0;
function OnTriggerEnter(collisionInfo: Collider): Collider
{
if(collisionInfo.gameObject.tag == "Player")
{if(enableTrigger == true)
{
t += Time.deltaTime;
GameObject.Find("Player").GetComponent("FPSInputController").enabled = false;
if(t>5)
{
GameObject.Find("Player").GetComponent("FPSInputController").enabled = true;
enableTrigger = false;
}
}
}
}
function TriggerEnable(){
if(enableTrigger == false)
{OnTriggerEnter.enabled = false;
t1+= Time.deltaTime;
if(t1>5)
{
OnTriggerEnter.enabled = true;
enableTrigger = true;
}
}
}
function Start(){
enableTrigger = true;
}
function Update () {
if(enableTrigger == true)
{OnTriggerEnter(GameObject.Find("Trap").collider);
TriggerEnable();}
}
Comment
Answer by Brenden-Frank · Dec 16, 2012 at 11:26 PM
A couple possible things,
Avoid using GameObject.Find and GetComponent unless its in a Start/Awake function. Make sure to check for null references before actually accessing them.
Is your Player (the object the player is controlling that has collision) actually tagged Player?
You're also calling OnTriggerEnter actively in your Update function. OnTriggerEnter is called by unity when the player enters a collider that you've checked as a Trigger.