- Home /
play a door animation with collider
so i have to 2 scripts that seem like they should work to me but since i have like no experience with scripting it may be completely wrong. i have a door that i animated to be like a slamming door. attached to that object i have this
var closeAnimationString : String;
function React()
{
animation.Play(closeAnimationString);
}
now i have a box collider with this this script
function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject.name == "door slam trigger")
{
gameObject.Find("girls door").SendMessage("React");
}
}
i get no errors but nothing happens when i enter this box. am i doing something way wrong? or is there a simpler way to do this?
You have onTriggerEnter on the object you're moving? Then this wont work. Because onTriggerEnter needs to be on the trigger itself. You could use OnCollisionEnter ins$$anonymous$$d.
Answer by aldonaletto · Aug 31, 2013 at 06:40 PM
In the code above, the box collider expects that an object named "door slam trigger" enters it. If the box collider is the "door slam trigger" object, you must instead compare the hit.name to the name of the object that enters it - supposing that the player is the "First Person Controller", do it like this:
function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject.name == "First Person Controller")
{
gameObject.Find("girls door").SendMessage("React");
}
}
@ vonni no the OnTriggerEnter is a box collider that i want to be the trigger and is not on the door itself.
and i changed the game object name like u said and it makes more sense now but now when i enter the box i get a nullreferenceexception NullReferenceException UnityEngine.GameObject.Send$$anonymous$$essage (System.String methodName) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:380) switcher.OnTriggerEnter (UnityEngine.Collider hit) (at Assets/Scripts/switcher.js:5) no idea what that means.
i ended up fixing the problem. the girls door was suppose to be capitalized because thats how it is in my hierachy. :)