- Home /
Other
OnTriggerEnter Not working in sample project
I have been working on the sample project: Space shooter but when I wrote the DestroyByContact script, the OnTriggerEnter is never called, I even tried adding a console command to see if it gets called but it doesn't.:
public class ExampleClass : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
Debug.Log("Triggered");
Destroy(other.gameObject);
Destroy(gameObject);
}
}
Both colliding objects have a Rigidbody and a Collider, with Is trigger checked, so I have no idea what is wrong.
EDIT: There's a second script on the object and integrating this code into that said script produces the intended result, so the issue only presists if there are two separate scripts. The unified script:
public float tumble;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.angularVelocity = Random.insideUnitSphere * tumble;
}
void OnTriggerEnter(Collider other)
{
Debug.Log("Triggered");
Destroy(other.gameObject);
Destroy(gameObject);
}
Answer by egerke · Feb 20, 2017 at 07:42 PM
Found the culprit. The problem was that I pasted another monobehaviour class inside the pre-created one from Unity's scripting refrence. So above the "public class ExampleClass : MonoBehaviour" line in the original code there was another similar class with a different name, that ended up containing it. Removed the extra class and now everything works.
Answer by jwulf · Feb 20, 2017 at 03:19 PM
Did you check the layers both objects are on, and whether collision detection between those is active or not in the project's Phsyics-Settings?
Both objects are in the Default layer, and collision is turned on for them in physics settings.