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 Oakie · Jun 03, 2012 at 05:13 AM · audioaudiosourcedialogueaudio.play

audio.Play(); not working while audio.Stop(); works fine when the audio is played on awake

In my game we have this dialogue sequence where you have some dialogue text and corresponding audio, and then you press space to hear/read the next line. I have this strange problem where I can't get audio.Play(); to work. There's a strange static sound I get for a second when the script is initialized and it goes through the audio.Play(); as if it is trying to play the audio but it isn't working.

 var dialogueStart : boolean = false;
 private var dialogue : int = 1;
 
 var dialogueBox : GUIStyle = new GUIStyle();
 
 var d1 : String;
 var d2 : String;
 var d3 : String;
 
 private var a1 : AudioSource;
 private var a2 : AudioSource;
 private var a3 : AudioSource;
 
 
 
 function Start(){
 
    var aSources = GetComponents(AudioSource);
    a1 = aSources[0];
    a2 = aSources[1];
    a3 = aSources[2];
      
 
 }
 
 function Update () {
 
 
   if (Input.GetKeyDown("space")){
     dialogue++;
   }
 
 }
 
 function OnGUI(){
 
    if (dialogueStart == true){
 
      Time.timeScale = 0;
 
      switch (dialogue)
      {
         case 1 :        
           GUI.Box(Rect(0,Screen.height*.8,Screen.width,Screen.height*.2),d1, dialogueBox);
           a1.Play();   
            break;      
 
         case 2 :      
           GUI.Box(Rect(0,Screen.height*.8,Screen.width,Screen.height*.2),d2, dialogueBox);
           a1.Stop();
           a2.Play();
         break;
 
         case 3 : 
           GUI.Box(Rect(0,Screen.height*.8,Screen.width,Screen.height*.2),d3, dialogueBox);
           a2.Stop();
           a3.Play();
         break;         
        case 4 : 
          a3.Stop();
          
        break;
      }
    }
 }

If I set the audio to play on awake, the audio.Stop(); works. I have seen someone else on the site have a similar problem. So, I tried the play on awake, and changing the volume method and that works, however by the time the audio is played it is in the middle of the audio and apparently you can't tell Unity to play an audio clip at a specific time so I don't think this method will work.

Any help or tips are much appreciated! (:

Comment
Add comment · Show 5
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 MD_Reptile · Jun 03, 2012 at 05:25 AM 1
Share

@Oakie might try checking if audio is playing, and if it is, then stop the previous audio, and then start the new audio, not sure if that will help or not.

avatar image Oakie · Jun 03, 2012 at 06:31 AM 0
Share

Good idea, I'll give it a go... Okay unfortunately didn't work, but I decided to throw some Debug.logs inside the .isPlaying if statements, and it made me realize that every single frame it is probably trying to call the audio, hence it not being able to actually play it. Okay I think I know what to do now, I'll update once I see if this works or not

avatar image MD_Reptile · Jun 03, 2012 at 06:35 AM 1
Share

Hmm, yeah gotta make sure to only to play or stop one time, ensuring you dont stop the next audio clip and such. Good luck!

Ohh also, if you dont set it to play on awake, then you script in something that happens after an amount of time or whatever, then Play() the audio, it should work that way. Another thing is, I would have a var for each audio clip, and then set them as public, and in the inspector assign each part of the audio, and Play() each one as you change the text on screen...rather than the way you just have audio[1] and such...

avatar image Oakie · Jun 03, 2012 at 06:50 AM 0
Share

Thank you for the help! It would have been very bad if I couldn't figure that out in the next few hours, so you really helped me out of this bind (:

avatar image MD_Reptile · Jun 03, 2012 at 06:53 AM 0
Share

No problem glad that I could help

1 Reply

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

Answer by Oakie · Jun 03, 2012 at 06:52 AM

Not sure of the protocol on this website concerning when a person is able to answer their own question, but I needed to make sure that the audio was only called upon once and not once per frame. So this was the end code that works, for anyone curious:

 var dialogueStart : boolean = false;
 private var dialogue : int = 1;
 
 var dialogueBox : GUIStyle = new GUIStyle();
 
 var d1 : String;
 var d2 : String;
 var d3 : String;
 
 private var a1 : AudioSource;
 private var a2 : AudioSource;
 private var a3 : AudioSource;
 
 var frameOnce : boolean = true;
 
 
 
 function Start(){
 
    var aSources = GetComponents(AudioSource);
    a1 = aSources[0];
    a2 = aSources[1];
    a3 = aSources[2];
    frameOnce = true;
      
 
 }
 
 function Update () {
 
 
   if (Input.GetKeyDown("space")){
     dialogue++;
     frameOnce = true;
   }
 
 }
 
 function OnGUI(){
 
    if (dialogueStart == true){
 
      Time.timeScale = 0;
 
      switch (dialogue)
      {
         case 1 :        
           GUI.Box(Rect(0,Screen.height*.8,Screen.width,Screen.height*.2),d1, dialogueBox);
           if (frameOnce == true){
             a1.Play();  
             frameOnce = false;
           }
            break;      
 
         case 2 :      
           GUI.Box(Rect(0,Screen.height*.8,Screen.width,Screen.height*.2),d2, dialogueBox);
           if (frameOnce == true){
             a1.Stop();
             a2.Play();
             frameOnce = false;
           }
           
         break;
 
         case 3 : 
           GUI.Box(Rect(0,Screen.height*.8,Screen.width,Screen.height*.2),d3, dialogueBox);
           if (frameOnce == true){
             a2.Stop();
             a3.Play();
             frameOnce = false;         
           }
         break;         
        case 4 : 
        if (frameOnce == true){
          a3.Stop();
        }
          
        break;
      }
    }
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can an object check on other objects? 1 Answer

master background music toggle not working after changing scenes 1 Answer

AudioSource.clip.time won't work? 2 Answers

Audiosource.Play() not working 0 Answers

Weird audio problem 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