Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by thornekey · Jul 14, 2013 at 01:34 AM · javascriptfpsdoors

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);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thornekey · Jul 14, 2013 at 04:39 AM 0
Share

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

avatar image Benproductions1 · Jul 14, 2013 at 04:41 AM 0
Share

Fixed it. Glad I could help :)

avatar image
0

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);
Comment
Add comment · Show 14 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thornekey · Jul 14, 2013 at 01:46 AM 0
Share

But then i get this error

No appropriate version of 'UnityEngine.Animation.Play' for the argument list '(UnityEngine.AnimationClip)' was found.

' even though its not "Animation.Play" im using..
avatar image create3dgames · Jul 14, 2013 at 02:22 AM 0
Share

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.

avatar image Benproductions1 · Jul 14, 2013 at 03:04 AM 0
Share

@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 :)

avatar image thornekey · Jul 14, 2013 at 03:25 AM 0
Share

@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..

avatar image Benproductions1 · Jul 14, 2013 at 03:28 AM 0
Share

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)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges