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 DimitriUK · Jun 28, 2014 at 12:30 AM · javascriptguiaudiotextloop

Playing multiple sounds with multiple subtitles

Hi,

I recently was working on getting an audio array to play, so when the user presses E on the object, it will loop through three different sounds.

I am now stuck again and have been trying to get three GUIText (Subtitles) to enable with the audio on each loop. So basically, I want three GUITexts to have their own looper and for the Alpha to become visable when the person is speaking, then if they press E again, the second text will appear, and so on.

Original Code

 public var soundClip : AudioClip[]; //to be assigned in inspector
 private var looper : int = 0;
  
     function OnTriggerStay (col:Collider){
  
          if(Input.GetKey("e")) {
             if(!audio.isPlaying)
             {
                      audio.clip = soundClip[looper];
                      audio.Play();
                      looper++;
                      looper = looper%soundClip.Length;
             }    
  
          }
      }


Stuck Code

 public var soundClip : AudioClip[]; //to be assigned in inspector
 public var gui : GUIText[];
 private var looper : int = 0;
 private var looperGUI : int = 0;
 
  
     function OnTriggerStay (col:Collider){
  
          if(Input.GetKey("e")) {
             if(!audio.isPlaying)
             {
                      audio.clip = soundClip[looper];
                      audio.Play();
                      looper++;
                      looperGUI++;
                      looper = looper%soundClip.Length;
             }    
  
          }
      }


So, if anyone has the time to help me, it would be appriceated as this is really bugging me. I don't mind using alpha changes or enabling/disabling the GUI text, you tell me what is best.

Comment
Add comment
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

1 Reply

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

Answer by Nerevar · Jun 28, 2014 at 02:32 AM

Hello again :p

This may work:

 public var soundClip : AudioClip[]; //to be assigned in inspector
 public var gui : GUIText[];
 private var looper : int = 0;
 private var looperGUI : int = 0;
  
  
     function OnTriggerStay (col:Collider){
  
          if(Input.GetKey("e")) {
             if(!audio.isPlaying)
             {
                      HideSubs();
                      audio.clip = soundClip[looper];
                      gui[looperGUI].enabled = true;
                      audio.Play();
                      looper++;
                      looperGUI++;
                      looperGUI = looperGUI%gui.Length;
                      looper = looper%soundClip.Length;
             }    
  
          }
      }

      function Update(){
         if(!audio.isPlaying)
         {
              HideSubs();
         }

      }

      function HideSubs(){
          for(var i = 0 ; i< gui.Length; i++)
          {
                gui[i].enabled = false;
          }

      }

Tell me if you have any unwanted behaviours, I did not fully tested it so some edition might be needed.

see you

Comment
Add comment · Show 3 · 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 DimitriUK · Jun 28, 2014 at 12:18 PM 0
Share

Hey again! Hahaha, thanks for responding so fast as well! I just tried doing your new script and there was no code errors, however it didn't work.

So, I went up to the object and pressed E, and it didn't even play a sound, nor bring up the GUI text that I assigned in the inspector. Do you think I need to give the GUI text a length or something? Because I'm not too sure how to do that.

Let me know what you think could be the problem and then I will change it. :)

avatar image Nerevar · Jun 28, 2014 at 01:04 PM 0
Share

$$anonymous$$mh, Well the audio should be playing I did not touched this part. You just need to assign the clips and the GUITexts from the inspector.

Do you have any errors on Play?

avatar image DimitriUK · Jun 28, 2014 at 01:19 PM 0
Share

You know what?! I don't know what it was, I think maybe the fact I had three elements of GUI and two of them were none, I changed it to 1 and now it works perfectly!! You are a genius, I was just being stupid hahaha! You seem really good in JavaScript, is there anyway I can contact you directly for advise? I'm eager to learn more about JS. :)

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gui Button Solid 2 Answers

GUI Text from script Java 1 Answer

gameObject does not function properly when used as an asset 0 Answers

Limit on GUI Components? 0 Answers

Detect Text in GUI; Print 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