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
1
Question by BjarkeDuDe · Oct 12, 2012 at 09:21 PM · javascriptbug

Why do i get 4 errors here? (Javascript)

Why do i get 4 errors in this? I even have the component with the errors in other scripts, that has no errors :(

 var Sound : AudioClip;
 var startTime;
 var timer1:int;
 
 
 function Update () {
 function Update (){TimerStart();}
 
     if(Input.GetKeyUp("f")) {
         audio.clip = Sound;
         audio.Play();
         function TimerStart (){
 
             startTime = Time.time;
             
             timer1 = Time.time; 
             
             if(timer1 > 1){
             audio.clip = Sound; 
             audio.Stop();
             
             }
         }
     }
 }
Comment
Add comment · Show 2
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 Noah-1 · Oct 12, 2012 at 09:23 PM 0
Share

post the errors please

avatar image FLASHDENMARK · Oct 12, 2012 at 09:25 PM 2
Share

You have a function Update within a function Update. That is not possible. And then you also got function TimerStart within the Update function which is not right either.

1 Reply

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

Answer by aldonaletto · Oct 12, 2012 at 09:31 PM

You should not declare functions inside functions, and startTime should be typed to avoid possible errors. I don't have the slightest idea of what you're expecting this code to do, but it could be written like this:

 var Sound : AudioClip;
 var startTime: float; // declare the variable type
 var timer1: int;
 
 function Update (){
   if(Input.GetKeyUp("f")) {
     audio.clip = Sound;
     audio.Play();
   }
 }
 
 function TimerStart (){
   startTime = Time.time;
   timer1 = Time.time; 
   if (timer1 > 1){
     // why do you set the audio.clip if you're going to stop it?
     audio.clip = Sound; 
     audio.Stop();
   }
 }
Comment
Add comment · Show 5 · 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 BjarkeDuDe · Oct 12, 2012 at 09:57 PM 0
Share

I am completely new to coding, i have no idea what i am doing, and i could use all the help i can get.

avatar image aldonaletto · Oct 12, 2012 at 11:37 PM 0
Share

Explain what you're trying to do, and we can help you to get it working.

avatar image BjarkeDuDe · Oct 13, 2012 at 09:44 AM 0
Share

I am trying to play a sound when i press the "f" key (Get$$anonymous$$eyUp). Then i set the timer so that after 1 second the audio stops. (The audio is 1 second long). Do i even need the timer? I am basically just trying to make it play once and then stop.

avatar image fafase · Oct 13, 2012 at 10:18 AM 1
Share

Fact is if you tick off Loop in the audio source then your audio file won't repeat. You do not need the timer.

avatar image aldonaletto · Oct 13, 2012 at 12:15 PM 1
Share

@fafase is right: a sound only repeat automatically if Loop is checked in the Inspector, thus you can delete the function TimerStart.

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

13 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

Related Questions

Why are these errors messing with me? 2 Answers

syntax errors 1 Answer

Is it the same javascript 1 Answer

Cant Add Script (or create any new scripts) Javascript 2 Answers

Why do sounds cancel out other sounds? 2 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