Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

OnTriggerEnter either not working or broken

Discussion in 'Scripting' started by fortunez, Jul 30, 2014.

  1. fortunez

    fortunez

    Joined:
    Aug 14, 2012
    Posts:
    9
    Hi everyone, I am having troubles with OnTriggerEnter. I have 4 prefabs with 2 colliders, a sphere collider (with trigger enabled) and a box collider. I have a script that enables another script that makes that object follow another.

    However, it does not work. It simply gets enabled as soon as I hit play.

    Code (JavaScript):
    1. var health : int = 100;
    2. var attackThreshold = 5.5;
    3. var attackRepeatTime = 1;
    4. var lockPos : float = 0;
    5. var Timer = 0.0;
    6. var target : Transform;
    7. var myTransform : Transform;
    8. function Awake(){
    9.     myTransform = transform;
    10. }
    11. function Update(){
    12.     var distance = (target.transform.position - this.gameObject.transform.position).magnitude;
    13.     if(distance > 2.1){
    14.         this.gameObject.animation.Play("ZFollow");
    15.     }
    16.    
    17.     transform.rotation = Quaternion.Euler(lockPos, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
    18.     if (distance < 2){
    19.         Timer += Time.deltaTime;
    20.         this.gameObject.animation.Play("ZAttack");
    21.         pControl.health -= Time.deltaTime;
    22.         Timer = 0;
    23.     }
    24. }
    25.  
    26. function OnTriggerEnter(){
    27.     this.gameObject.GetComponent("SteerForTarget").enabled = true;
    28.     this.gameObject.GetComponent("AutonomousVehicle").enabled = true;
    29.    
    30. }
    Any idea how to fix?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,935
    Try printing to see what it hits:
    Code (JavaScript):
    1. function OnTriggerEnter (other : Collider) {
    2. Debug.Log(other.transform.name);
    3. }
     
    fortunez likes this.
  3. fortunez

    fortunez

    Joined:
    Aug 14, 2012
    Posts:
    9
    Hey, it's showing up every single collider there is around the object.
    I should use tags to make sure it's only the player?

    Edit:

    Fixed now haha, haven't used Unity in a while so iv'e forgotten some stuff ^^