- Home /
OnTriggerEnter not being called, have trigger, RB, and colliders set?
Hi!
I have a GameObject that is supposed to set a boolean on its animator to true once a trigger is called. Thing is, I debugged it with print() and found that it was never called? Both the objects have 3D colliders, and one of them has a rigidbody. I have the object that needs to be collided with set to trigger. I triple-checked that my trigger Gameobject's tag is spelled correctly as well. I am lost on what I could be missing. Please help? Thanks.
Here is the code for the trigger.
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == ("PlayerAttack"))
{
health = health - 40;
print(health.ToString());
if(health > 0)
{
Anim.Play("GhostLightHit");
Anim.Play("GhostIdle");
}
else if(other.gameObject.tag == ("EnemyTarget"))// The script part that isn't working
{
print("Collided");
Anim.SetBool(Attacking, true);
Anim.SetBool(canMove, false);
Audio.clip = otherClip[Random.Range(0,otherClip.Length)];
}
else
{
Instantiate(GhostDead, transform.position, transform.rotation);
Destroy(gameObject);
}
}
}
we cant tell you you will need to provide us with screenshots or something
Caught one bug and changed code so the other... EnemyTarget")) is now after the else statement, as I can't have 2 tags. Still does not work, however.
Sorry about that. I just updated the question with pictures.
Answer by Pathojen · Aug 02, 2020 at 11:03 AM
I didn't exactly figure it out, but I found a workaround. I added a private boolean canMove, and made it so that it switches to false OnTriggerEnter. Everything in the section for the part that doesn't work has been placed in an if statement to test if the new bool is true or not. If not, then the part that was ignored previously takes place.
Still curious on what I did wrong in the original code, though. There has to be a more effective way of doing things.
Your answer
Follow this Question
Related Questions
OnTriggerEnter/Exit called unexpectedly 2 Answers
Access other collider without use of Trigger collider. 1 Answer
Can't get trigger to work 1 Answer
OnTriggerEnter fails to activate 1 Answer