- Home /
What's wrong with this JavaScript door script?
Hey guys, i made this script so that i wouldnt have to recreate scripts to open doors everytime. just drag in the animations and sounds. anyway its not working but im not getting any errors either.. what could be the issue?
var dooranimationopen : AnimationClip;
var dooranimatioclose : AnimationClip;
var dooropensound : AudioClip;
var doorclosesound : AudioClip;
function OnTriggerEnter(c: Collider)
{
if (c.tag == 'Player') {
animation.play(dooranimationopen);
audio.PlayOneShot(dooropensound);
}
}
function OnTriggerExit(c: Collider)
{
if (c.tag == 'Player') {
animation.play(dooranimatioclose);
audio.PlayOneShot(doorclosesound);
}
}
Answer by Benproductions1 · Jul 14, 2013 at 04:29 AM
Hello,
Now that we have narrowed down the problem, finding the solution is easy XD
Looking at the documentation, AnimationClip
does not have a "name" variable (even through it does) meaning that this is undocumented and should not be used.
My suggestion is to iterate through all the animation states to find the matching clip (If you really want to add the clip to the script and not just the name)
//Please use CamelCase, like everyone else :)
var doorOpen:AnimationClip;
var doorClose:AnimationClip;
private var openState:String;
private var closeState:String;
//This iterates through all animations
//attached to the animation component
//If the clip of one of the states matches the clip of
//one of the variables, we reference the name
function Start() {
for (var state:AnimationState in animation) {
if (state.clip == doorOpen) {
openState = state.name;
}
else if (state.clip == doorClose) {
closeState = state.name;
}
}
}
//Here we use the name we gathered in Start
//To run the appropreate animations
function OnTriggerEnter(c: Collider) {
if (c.tag == 'Player') {
animation.Play(openState);
}
}
function OnTriggerExit(c: Collider) {
if (c.tag == 'Player') {
animation.Play(closeState);
}
}
Easy as goo pie,
Benproductions1
Brilliant! that worked really well! thanks heaps for all your help man :) except i thought it didnt work cos you didnt put the "p" for "animation.Play" in capitals. but not to worry its all good :)) thanks again for the help
Answer by create3dgames · Jul 14, 2013 at 01:41 AM
Should be animation.Play
not animation.play
.
See below
INCORRECT:
animation.play(dooranimationopen);
CORRECT:
animation.Play(dooranimationopen.name);
But then i get this error
' even though its not "Animation.Play" im using..No appropriate version of 'UnityEngine.Animation.Play' for the argument list '(UnityEngine.AnimationClip)' was found.
Okay, I edited my answer. Turns out Unity is looking for the animation as a string, so you have to do dooranimationopen.name
. And of course Play still has to be capitalized.
@create3dgames That only works if the clip is attached to the Animation
component. From what I gather, the OP has no knowledge of how to use that component :)
@Benproductions i have the animation clip attatched to the component. the two animations are "BioLabDoorOpen" and "BioLabDoorClose" but you see in the script the variable is dooranimationopen so when i drag the animation clip (biolabdoor..) onto the dooranimation spot in the inspector thats all good but they wont animate..
First thing to check is if the trigger really gets called. Does the sound play?
If so, then you should get an error if there is something wrong with the animation. (<- that or it should play)
Your answer
Follow this Question
Related Questions
FPS Controller problems 4 Answers
Accessing variables from scripts issues 1 Answer
How to let my gun shoot ?? 0 Answers
UnityEngine has no appropriate version problem 1 Answer
How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers