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 Lukemwill · Mar 18, 2015 at 05:42 PM · audioaudiosourceaudioclipaudio.playoneshot

Problems combining Audio triggers, PlayOneShot and Audio Arrays

I am having problems with audio triggers, Audio arrays and PlayOneShot. Ultimately I want to have an audio source with multiple audio clips that once a player walks through the trigger, will play a random clip from an audio array. However, once the random audio clip has played I don't want the audiosource to trigger again. I have a script for the PlayOneShot trigger, and another for the audio array. I want to combine the two or have two separate but functional scripts to do this.

The audio trigger script is as follows

 var sounds : AudioClip;
 
 private var hasPlayed = false;
 
 function OnTriggerEnter(){
     if(!hasPlayed) {
         GetComponent.<AudioSource>().PlayOneShot(sounds);
         hasPlayed = true;
     }
 }

and the audio array code is as follows (i have only two clips in at the moment)

 var sounds : AudioClip[];
 var LittleGirl : AudioSource;
 var ManyWhispers : AudioSource;
 
 LittleGirl.clip = sounds[Random.Range(0, sounds.length)];
 LittleGirl.PlayOneShot;
 ManyWhispers.clip = sounds[Random.Range(0, sounds.length)];
 ManyWhispers.PlayOneShot;
 

Whether this is right or not I do not know since I am very knew to scripting. It seems that I cannot sue audio arrays and PlayOneShot and I cannot seem to combine the two scripts. Can someone please help me with a walkthrough of some kind. if there's any more information you need then please do ask!

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

Answer by tebandesade · Mar 18, 2015 at 06:22 PM

Hi! What about if you at the beginning define public var sounds : AudioClip[] an in the editor you just drag and drop the audios that you want to be included in the array. Afterwards when you're about to play the sound because of the trigger do something like int x = Random.Range (0, a.Length); and .PlayOneShot(sounds[x]);

Comment
Add comment · Show 4 · 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 Lukemwill · Mar 18, 2015 at 06:29 PM 0
Share

Could be a solution! Could you give me that example in a code? I'm really not good at deciphering the order of where to put things in a script :(

avatar image tebandesade · Mar 18, 2015 at 07:13 PM 0
Share
         var  sounds: AudioClip[] ;
         private var hasplayed = false ;
         
 function OnTriggerEnter(){
      if(!hasplayed)
      {
          var x = Random.Range (0, sounds.Length);
          GetComponent.<AudioSource>().PlayOneShot(sounds[x]);
          hasplayed = true;
      }
      }

Idk something like this maybe. And in Unity you have your audios and you'll se in the script component that a field size appear, you just modify that field for the amount of sounds you want and add them.

avatar image Lukemwill · Mar 18, 2015 at 07:25 PM 0
Share

Okay thank you, i'll try it out and post the results!

avatar image Lukemwill · Mar 18, 2015 at 09:15 PM 0
Share

Works like an absolute dream! Thanks so much!

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

How to switch Audiosource without stacking by an input? 0 Answers

Audio does not finish playing whenever I press another key 0 Answers

2nd Audio Clip Not Playing 0 Answers

Looping an audio while holding "shift key" 1 Answer

Why PlayOneShot is not working? C# 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