Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KrishnaG · Dec 20, 2012 at 02:52 AM · audiowaitcommunication

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image infinitypbr · Dec 20, 2012 at 02:59 AM 0
Share

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.

avatar image VicM · Dec 20, 2012 at 03:16 AM 0
Share

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?

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VicM · Dec 20, 2012 at 02:42 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alec Thilenius · Dec 20, 2012 at 04:04 AM 0
Share

As others have said also, if you have a scripting problem, then we need code to offer useful help.

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Playing audio when a button is clicked? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges