OnTriggerEnter Destruction |Solved|

I am making an FPS where the player has a flamethrower and must destroy trees. I am doing this by putting an invisible cube acting as a trigger, tagged “Fire”, in front of the gun so when a tree collides with it it will be destroyed. I’ve tried two different methods, and I am at a total dead end.

Here is my code, (JS),

function OnTriggerEnter (hit : Collider)


if(hit.gameObject.tag == “Fire”)

	renderer.enabled = false;
    collider.enabled = false;



Above is attached to my tree. Please help, I’m new at this

You could also try apply a World Particle Collider and set “Send Collision Message” to true. Then on your tree you check for particle collisions:

private var health = 100;
private var isBurning : boolean = false;
function OnParticleCollision () {

    if(health<=90 && !isBurning){
        GetComponent(ParticleEmitter).emit = true;
        renderer.enabled = false;
        collider.isTrigger = true;

Just for fun you could (as in the provided example) add a health and a second particle emitter to the tree so it really is burning.

That script will cause the tree to disappear only if it enters the flame trigger. This means that, if the tree is already touching the trigger, there will be no entering action.
Unfortunately neither OnTriggerStay will work, because in order to detect a trigger (or any kind of collision), one of the two colliders has to move at least a little bit.

Why don’t you try a Raycast instead? Something like this:

var hit : RaycastHit; //Raycast informations
var treeMask : 1<<10; //LayerMask for trees. "10" will be the layer number for trees (any other value will do)
var firing : false; //Is the weapon firing?
var fireRange = 10; //Fire range of the flamethrower

	//If the weapon is firing against a tree...
	if (firing == true && Physics.Raycast(transform.position,fwd,fireRange,treeMask)){
		//Set the tree on fire!
		collider.GetComponent(TreeScript).onFire = true;

First of all, you must determine when the weapon fires.
You will then need a simple script (TreeScript) attached to the tree that, when its variable onFire is true, will make the tree disappear (and eventually spawn some fire particles).

Other people bring up good points about your script, but the biggest problem is that

collider.enabled = false;

doesn’t exist. You can’t enable/disable colliders. You can however turn them into a trigger, which will make the physics engine ignore them for collisions.

collider.isTrigger = true;