- Home /
OnTriggerEnter with variable
I'm trying to create a code that accesses an external collider but all I'm getting is "The name does not denote a valid type (not found).
var colliding : Collider;
function OnTriggerEnter (collider : colliding) {
What am I doing wrong?
Answer by Gamershaze · Aug 16, 2015 at 03:55 AM
You're using the C# format, where the type is listed first and not second. Here's a UnityScript version below.
function OnTriggerEnter (colliderName : Collider) {
Destroy(other.gameObject);
}
Here you can see the Collider type is denoted second, while the name you specify for the collider is first. You don't need to create a variable for the collider either, as it's handled in-function as a local variable.
Here is the API for the collider class as well, for more usage. http://docs.unity3d.com/ScriptReference/Collider.html
Answer by ransomink · Aug 16, 2015 at 06:12 AM
I'm guessing, you need to access a collider outside of the trigger event? If so, you can do this:
#pragma strict
var externalCollider : Collider;
function OnTriggerEnter( col : Collider )
{
externalCollider = col;
}
Now that you have a reference, you can use the collider from the event for anything you need. Hope this helped...
Your answer
![](https://koobas.hobune.stream/wayback/20220612000045im_/https://answers.unity.com/themes/thub/images/avi.jpg)