function OnTriggerExit()

I’ll use an image to try and convey my problem first.

KEY

Red: Turret
Blue: Cannonball
Green: Turret Sphere Collider

What I’m attempting to achieve here is to make the Cannonball destroy on exiting the Sphere Collider.

This is what I have so far:

function OnTriggerExit(other : Collider)
{
	if(other.gameObject.transform == myTarget)
	{
		myTarget = null;
	}
}

The ‘myTarget’ is for an object moving in and out of the collider. This is so that the turret knows not to aim at ‘myTarget’ upon exiting the collider.

I tried adding:

if(other.gameObject.transform == myProjectile)
{
Destroy(myProjectile);
}

but it didn’t work.

myProjectile is of type Transform, as is myTarget. I tried messing around with the “is Trigger” options, to no avail.

Any advice?

Change your code to:

if(other.gameObject.transform == myProjectile)
{
Destroy(myProjectile.gameObject);
}

Destroy only works with GameObjects, usually wont work with a Transform.

You need to make sure that your sphere collider is set to trigger = true.
An alternative method for this would be to compare using an overlap sphere.

Look up Physics.OverlapSphere in the unity docs. I have a code example at home that I will post if you need it, but I’m at work atm.