- Home /
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();
}
}
}
}
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.
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();
}
}
I am completely new to coding, i have no idea what i am doing, and i could use all the help i can get.
Explain what you're trying to do, and we can help you to get it working.
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.
Fact is if you tick off Loop in the audio source then your audio file won't repeat. You do not need the timer.
@fafase is right: a sound only repeat automatically if Loop is checked in the Inspector, thus you can delete the function TimerStart.
Your answer
Follow this Question
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