Stealth - Chapter 1: 6. Game Controller...

Hi guys, I’m quite new here. I’ve always been more of a modeller/designer but am taking the plunge into programming and scripting which I am loving. I’m using JavaScript by the way.

I’m working my way through the Stealth tutorial series and have come across something that I don’t understand. He makes a reference to the panic alarm music like this:

private var panicAudio : AudioSource;                                       // Reference to the AudioSource of the panic msuic.

Which to my knowledge is just declaring a new variable of type AudioSource. (?)
He later implements the variable:

// ... and fade in the panic music.
        panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);

but I don’t see anything that’s linking the actual AudioSource of the child Game Object, which is called secondaryMusic.

What am I not understanding?

Here’s the link: The Lesson and Code

Thanks so much guys, looking forward to your wisdom!

Your link is out, can you update it ?

In the C# code, line 30 initializes panicAudio.

1 Answer

1

The variable is valuated at this line :

// Setup the reference to the additonal audio source.
panicAudio = transform.FindChild("secondaryMusic").audio;

It finds a child called “secondaryMusic” in the game object containing this script.
So every use of the variable panicAudio after this line calls a method or a variable of secondaryMusic.audio, which is an AudioSource.

Of course! That makes total sense, I was just being an idiot. I stared at the code for 30 minutes and somehow I just did not see that line. Thank you so much Kira!

This kind of things can happen to anyone working a lot on some code part, the obvious can be not so obvious ... To avoid that kind of problem, you can use [this method][1]. It can seem stupid at first, but it can sometimes help you avoid some little bugs, or find obvious stuff when you do it correctly :) Good luck with your game ! [1]: http://en.wikipedia.org/wiki/Rubber_duck_debugging