script problems (with sound)

Hi…

Surely I´m confused with a very simple script…

I´m trying to play a sound event or music when the fps controller touchs a trigger…

Here is that I have done:

  1. I have created a plane (simulating a ground) and a cube, this last one have it as a trigger (with physics, box collider).

  2. In the Trigger, i have this javascript:

var music : GameObject;
var play_music = false;

function Update () {

if (play_music){
	music.audio.Play();}}
	
function OnTriggerEnter(other : Collider){
play_music = true;}

As a novice i´m, i can´t see some class of error there…

Then, as the script is looking for a GameObject containing the Audio Source, i have put the Audio Source in the Ground, so i was aiming the “Ground” to the script (GameObject) in the inspector.

The problem is when I play the game and pass over the trigger, the function OnTriggerEnter works, (i can see how checkbox activates in the inspector panel) but i can´t listen anything, the music does not play, but strangely, (when playing) if I deactivate the “play_music” variable in the inspector, the music starts to play…

how can I solve it…

thanks !

Hi again, here is a little demostration of what i´m talking about:

if you download and open this project, you will understand fastly what is the problem…

http://www.missupload.com/xum7tq7102mv/Testing.rar.html

If you keep the Ground´s inspector on screen, and play the project, then you must to go to collide with the cube in front, you will see that the checkbox in the inspector enables (play_music), but isn´t playing…

but while playing, if you clic the checkbox to disable the variable, the music will start… strange… the script is wrong?..

thank you in advance…

[/url]

It looks to me that once play_music goes true, every frame Play() gets called.

Why not just do this instead:

function OnTriggerEnter(other: Collider)
{
   if (!music.audio.isPlaying)
       music.audio.Play();
}

yeah, it’s works good, so can i set this code without declaring variables and function Updates in every object i want to put a music right?

I understood what you talking about, if i put the play event inside the Update function, the music does not work… all the sounds calls must be made in OnTriggers functions… all clear now…

thank you !

For future reference, you dont set a true/false flag by declaring
var play_music = false;

You’re trying to set a boolean but you haven’t told the engine that, so use

js version---
var play_music:boolean = false;

cs version---
bool play_music = false;

Then that allows you to set true/false values as you wish throughout your code.