Marble tutorial soundtrack

Hi there,

newbie here,

I'm tryig to add a kind of dynamic soundtrack to the marble tutorial.

the idea is to add a new audioclip every time a ball falls.

I have made 3 audioclips that are synchronized to play together. I have made 3 ParticleSystems (called: PS1, PS2 and PS3) that have an audiosource to put each audioclip. When the game launches the audioclips are playing with audio.play() but you can hear only the first, cause I have attached a script called "vol" to the other 2, to mute the sound. This mute script is:

"var x=0;
audio.volume=x;"

so, the idea is when the Player's score is 1, to set the x = 1, in order to hear the second audioclip!!

I opened the tutorial's score script and I added the following in bold:

var score = 0;
function AddToScore () {

++score;

guiText.text = "Player 1: " + score.ToString ();

if (score=1) {
** GameObject.Find("PS2").GetComponent(vol).x= 1;**

}

} [/b]

....but nothing happens :cry:

I'm totally new with Unity and my Javascript knowledge is a bit limited, so any help or suggestion would be appreciated!!

anyone?? :sweat_smile:

Currently you are setting the variable x of the vol script to 1, which does not automatically set the volume of the associated AudioSource. I don't have Unity in near me at this time, but you might want to do something like:

// Script on audio object
var audio : AudioSource;

function Start () {
  // If the audio variable was not set in the inspector set it now.
  If (!audio) {
     audio = gameObject.GetComponent(AudioSource);
  }
}

function SetVolumeToValue(newVolume : float) {
  audio.volume = newVolume;
}

function TurnVolumetoFull() {
  SetVolumeToValue(1.0);
}

function TurnOffVolume() {
  SetVolumeToValue(0.0);
}

Then from your other script

if (score=1) {
  GameObject.Find("PS2").GetComponent(vol).SetVolumeToFull();
}

thanx a lot for your interest. it doesn't seem to work though. I don't completely understand the SetVolumeToValue function, I searched in unity's scripting reference but didn't find something relevant to it. Where could I get more information about it??

Also, the (score=1) parameter of if doesn't seem to be right cause I get an error that it cannot recognize the = sign.

The SetVolumeToValue() is a function that I made. It is just calls a Unity function to set the volume to whatever value it pass it. Other things could be done in the function, although it is not really needed. You could set the volume directly in the other functions. You don’t even need a script for that matter. You could just set the volume directly from your other script.

GameObject.Find("PS2").GetComponent(AudioSource).volume = 1.0;

The “=” should be “==”.

Thank you! Thank you!! Thank you!!!

Is it maybe possible to make something like a fade in, cause you can hear a "click" when the volume is going back to 1. For example something like set volume=0.2, wait for a frame, set volume=0.4, wait for a frame etc.

You can use a yield statment and a loop, such as

for (i=.2, i<= 1.0, i=i+.2) {
  audio.volume = i;
  yield;
}

This loop will cause the volume to go up .2 each frame, at least I think it will :)