- Home /
Making the script wait for a button click and then playing the audio file
Hi all,
I have an array of audio files, which I play in sequence. After a few of them are played, I need my script to wait for a button to be clicked (another script controls the button)
I am able to play my audio files in sequence, but the communication between the two scripts isn't working properly.
I have tried using static variables in both the scripts, but they don't seem to help.
Can someone please help me get a solution for this problem?
I'm not the sharpest tool in the shed, and I supposed I'd need to see your code to make a real guess. However, you could probably use PlayerPrefs? Set the PlayerPref on one script, and read it on the 2nd.
You're communicating using a function? If so then you can just pass the variables through the function call.
Please add some code, or be more specific so we can try to help you.
What you mean by the communication between the scripts is not working?
Answer by tw1st3d · Dec 20, 2012 at 06:28 AM
function Start(){
var var1 : int = 50; /* Distance from left */
var var2 : int = 50; /* Distance from top */
var var3 : int = 100; /* Width */
var var4 : int = 50; /* Height */
var clicked : boolean = false;
public var audiosource : Audio;
}
function OnGUI(){
if(GUI.Button(Rect(var1,var2,var3,var4), "Click to play")){
clicked = true;
}
}
function Update(){
if(clicked == true){
audiosource.Play();
clicked = false;
}
}
That is the main idea, but I would suggest not using the "clicked" true flag. Why not to just trigger the audio clip right inside the button click condition?
The other thing in that as far as i now OnGUI and the Update functions are called different times per frames so your audio play will not be synced at all.
Also with that code make sure that you have a clip attached to the audio source which is not shown there. Refer to the "AudioSource.clip " unity documentation example as additional assistance.
Besides that, what tw1st3d states is the way to go.
$$anonymous$$ore details about your problem and your code will give us a better hint @$$anonymous$$rishnaG.
Answer by Alec Thilenius · Dec 20, 2012 at 04:02 AM
Use the built in messaging system in Unity. When I started on Unity3D I was very hesitant to use it. I have performance concerns, and it didn’t seem as ‘clean’ as I would have liked. It wasn't until I started reading a lot of game engine architecture books that I started to realize the value of a messaging system similar to what Unity has. I now use messaging for the vast majority of script communication, and it has allowed for a very nice ‘loosely bound’ setup.
To learn about Messaging, take a look at: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html
As others have said also, if you have a scripting problem, then we need code to offer useful help.
Answer by KrishnaG · Dec 21, 2012 at 12:51 AM
Ah, never mind, I got the solution to my problem. I had the name of the other script wrong. Thanks for the help guys!
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Problem with yield waitForSeconds in for loop 0 Answers
How to add Elements? Multiple ones!????? Help PLease 1 Answer
Wait Until Sound is Finished 2 Answers