- Home /
Hi. I am having trouble with a script. I am trying to make an object disappear when the object itself enters a trigger. Here's the script I have below.
var object : GameObject;
var sound : AudioClip;
function OnTriggerEnter(Target:Collider)
{
if(Target.tag == "LurkerEnd")
{
Destroy(object);
audio.PlayClipAtPoint(sound, transform.position);
}
}
Answer by robertbu · Aug 05, 2014 at 12:04 AM
If I understand correctly, change line 8 to:
Destroy(Target.gameObject);
If it does work, put a Debug.Log() just inside your OnTriggerEnter(). That will tell you if you have an issue getting the trigger to work, or if you have a tag issue. And you don't need 'object' after this change.
When I add the Debug.Log() to the OnTriggerEnter() function, it says: Assets/Scripts/triggerDestroy.js(5,20): UCE0001: ';' expected. Insert a semicolon at the end. So I put a semicolon at the end of Debug.Log(), and now it says: Assets/Scripts/triggerDestroy.js(5,18): BCE0023: No appropriate version of 'UnityEngine.Debug.Log' for the argument list '()' was found.
Here's the script now:
var sound : AudioClip;
function OnTriggerEnter(Target:Collider)
{
Debug.Log();
if(Target.tag == "LurkerEnd")
{
Destroy(Target.gameObject);
audio.PlayClipAtPoint(sound, transform.position);
}
}
You must give it a string to log. In this case, something like:
Debug.Log("Hit Trigger");
The string will be printed in Console when the function is ran.