- Home /
Unity Giving Errors after adding "Private"
Why does Unity want a } at 8/9 and want EOF at 22/9? All I did was add "private" to the variable. Thanks as always.
var walls : GameObject;
var animationStopped : boolean = false;
var Mineshaft: AudioSource;
var Halt: AudioSource;
function Start () {
private var aSources = GetComponents(AudioSource);
Mineshaft = aSources[0];
Halt = aSources[1];
animation.Play();
Mineshaft.Play();
yield WaitForSeconds (animation["Elevator_Move"].length);
animationStopped = true;
}
function Update () {
if (animationStopped) {
Mineshaft.Stop();
Halt.Play();
Destroy(walls);
}
}
Answer by AlucardJay · Jun 24, 2013 at 01:50 AM
You do not declare public or private for local variables.
Local variables only exist in the function they were declared in, and only for that frame. They cannot be seen or accessed by other functions.
There is absolutely no reason to mark a local variable as public/private, only Global variables :
public var globalVariable1 : float;
private var globalVariable2 : float;
function DoStuff()
{
var localVariable : float;
}
function DoMoreStuff()
{
var localVariable : float; // same name, no errors, local variables only exist in the function they were created in, and only for that frame
}
Okay I understand. I assumed private served a different purpose.
Solved. But then I get these:
Array index is out of range.
Non matching Profiler.EndSample (BeginSample and EndSample count must match)
Ins$$anonymous$$d of starting a whole new question: any quick ideas?
I've never put two audio sources on an object, I didn't think it was possible. I need to do some experimenting, but for what you're doing, consider just changing the audio clip on the audio source :
@script RequireComponent(AudioSource)
var walls : GameObject;
var $$anonymous$$ineshaft : AudioClip;
var Halt : AudioClip;
function Start ()
{
animation.Play();
audio.clip = $$anonymous$$ineshaft; // set the audio clip to $$anonymous$$ineshaft
audio.Play();
yield WaitForSeconds (animation["Elevator_$$anonymous$$ove"].length);
animationStopped = true;
}
function Update ()
{
if (animationStopped)
{
audio.Stop();
audio.clip = Halt; // set the audio clip to Halt
audio.Play();
Destroy(walls);
}
}
I need to do some experimenting, to see if one can put two audio sources on an object, and if that's how you really want to do it.
Edit : you could do all this in Start :
@script RequireComponent(AudioSource)
var walls : GameObject;
var animationStopped : boolean = false;
var $$anonymous$$ineshaft : AudioClip;
var Halt : AudioClip;
function Start ()
{
animation.Play();
audio.clip = $$anonymous$$ineshaft; // set the audio clip to $$anonymous$$ineshaft
audio.Play();
yield WaitForSeconds (animation["Elevator_$$anonymous$$ove"].length);
audio.Stop();
audio.clip = Halt; // set the audio clip to Halt
audio.Play();
Destroy(walls);
}
Perfect as always. This is a whole lot cleaner and I will be using this method in the future.
You can assign two audio sources by using the component menu in the inspector (Unity 4). Use the "audio" tab.
Your answer
Follow this Question
Related Questions
Assign prefab to variable through script 1 Answer
[4.6 UI] Show text as an int variable error 2 Answers
Can't access prefab's variables, once instantiated. 2 Answers
HELP! is this a Bug in the game engine? 1 Answer
error CS0841: A local variable `holdSound' cannot be used before it is declared 1 Answer