using UnityEngine;
using System.Collections;
public class OnTriggerEnter : MonoBehaviour
{
OnTriggerEnter(Collider other)
{
Destroy(other.gameObject);
print("Detected");
}
}
Object with script has rigidbody, and box collider with ontrigger ticked.
Object thats gonna hit it has rigidbody, and box collider without ontrigger ticked.
If this is Javascript, you’re missing “function” at the beginning of the method definition. If it’s C#, then you’re missing the return type “void”. I’d be surprised if this wasn’t generating an error.
Check the Physics collision matrix too, in Edit => Project Settings => Physics and make sure the two layers can collide with eachother. Also, if they’re both children of the same object which also has a collider, they’ll create a “compound collider” and won’t be able to collide with one-another.
Name your class something different. Right now your OnTriggerEnter method is a constructor for the class (which is why it compiles) and not using unity’s trigger event. Mono-behavior does not run class constructors, to my knowledge - you can’t create them with the new keyword.
Putting void in front of your method MIGHT work, but your renaming your class is definitely a better route.
if you rename the class and do nothing with the “function” it’ll error, it’ll no longer be a constructor and it’s not got a return type for a function declaration…
Well yes, renaming it ALSO requires that you add a return type (void) so that it becomes a proper method. But at least you will get an error message in compilation hinting at that.