- Home /
OnTriggerEnter fails to activate
I have an object tagged as "Buster" flying through a trigger that should detect objects with the tag "Buster" and respond, but it does not. I have seen in other similar questions asked here the solution was found when they added a rigidbody to one of the objects. So I have added a rigidbody to the "Buster". Raycasts Hit Trigger is true. The trigger is a sphere collider with Is Trigger set to true.
This is the trigger script:
#pragma strict
public var HP : int;
function Start () {
}
function Update () {
}
function OnTriggerEnter (other : Collider) {
if(other.tag == "Buster"){
HP = (HP -1);
}
}
This is the "Buster" script:
#pragma strict
private var left : boolean;
public var time : float;
public var speed : float;
private var counter : float;
public var xPosition : float;
public var yPosition : float;
function Start () {
transform.position.x += xPosition;
transform.position.y += yPosition;
left = true;
counter = 0;
}
function Update () {
if (left) {
if (time > counter){
transform.position.x -= speed;
counter++;
}
else {
Destroy(this.gameObject);
}
}
else {
if (time > counter){
transform.position.x += speed;
counter++;
}
else {
Destroy(this.gameObject);
}
}
}
Did you try adding a Debug.Log message in OnTriggerEnter to see if it were firing?
I can think of two things. 1, the trigger is so think/object is moving so fast, that when the object is passing through the trigger, Unity isn't "looking" at it. 2, typo perhaps at the tag?
I can see it isn't firing because the HP variable isn't lowering. I have the Inspector window up as I play test. I did a look over on the tag and script, there is no typo on the tag.
It could possibly be going too fast for it. I tried slowing it down to a crawl and it still isn't being triggered.
Thanks for the quick responses, I appreciate it!
try other.CompareTag("Buster") ins$$anonymous$$d of other.tag
Answer by Teyandee · Jul 07, 2014 at 11:06 PM
I am not sure what I did but I accidentally fixed it I guess! I got done eating dinner, rebooted Unity... and well.. its working. Thanks to everybody helping me, again I really do appreciate it!