Need help detecting when a collision ends

I need help writing a script. I’m trying to make a simple script where when the player rolls onto a block, the block lights up (by creating a point light above it); and when the player rolls off the block, it returns to normal (by deleting the point light). I have the first part working but when the player rolls off the block, the block stays lit up. Also if the player rolls back onto the block it creates another point. Here is my script so far.

#pragma strict

var blockEffect : Transform;


function OnTriggerEnter (info : Collider)
{
	if (info.tag == "Player")
	{  
	    Debug.Log ("Player is colliding");
	    var effect = Instantiate(blockEffect, transform.position, transform.rotation);
		//This makes the point light
		Destroy(effect.gameObject, 10);
		//This destroys the point light after 10 seconds, but i want it to destroy it when the player stops colliding with the block.
		Destroy(gameObject, 30);
	}
   
}


function OnTriggerExit (info : Collider)
{
	if (info.tag == "Player")
	{  
	    Debug.Log ("Player is not colliding");
	}
}

Any help would be greatly appreciated! I am new to the unity community and Javascript so I apologize if this is a newbey question.

Thanks!
Micah

you have to move your Destroy(effect.gameObject) inside the if statement of OnTriggerExit and take the effect variable out of the OnTriggerEnter like so…

var effect : GameObject;

function OnTriggerExit (info : Collider)

{

    if (info.tag == "Player")

    {  
        
        Debug.Log ("Player is not colliding");
        Destroy(effect); // <-- right here
    }

}

this should work :slight_smile:

Thanks for the quick reply! :smile: I tried your script and i get this error in the console when the player collides with the block - “Destroying assets is not permitted to avoid data loss.” I’m not sure why it wont let me delete it. It worked fine when i was just using the 10 second countdown timer to delete it. If you could help that would be great!

Thanks!:slight_smile:

Ohhh! I see! I got it working :smile: Thank you so much! :slight_smile: