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
1
Question by MOLB · Jan 25, 2012 at 08:51 PM · audiosound

How to use audio commands and Audio Source?

Hi

I have some questions about sound in Unity.

1: How do I use a Audio Source? Can I put a empty Audio Source on an object and then put use a scrpit to control sounds? I play different AudioClip variables.

2: What more functions regarding sound are there than PlayOneShot? I have tried Play and Stop, but it does not work as planned.

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

2 Replies

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

Answer by AlucardJay · Jan 26, 2012 at 07:08 PM

Hey, I am new to unity and not much of a programmer, but I have the same problem. I think I can answer one part.

I created an Empty gameObject, gave it the component Audio Source (untick play on awake, and leave the audio clip box at None / empty). Then I applied this script. (Don't forget to add the sounds to the prefab / object in the inspector after you have attached the script and component audio source).

 // random sounds script
 // ----
 
  
 // Inspector Variables
 
 // Sounds
 
 var scream1 : AudioClip;
 var scream2 : AudioClip; 
 var scream3 : AudioClip; 
 var screamSound : AudioClip;
 var randomSound : int = 0;
 
 // ----
 
 // Game Loop
 function FixedUpdate ()
 {
     // when you want to play a sound (one shot) , call the function
     // playSound ();
 }
 
 // ----
 
 //
 function playSound ()
 {    
     randomSound = Random.Range(0,10);
 
     if ((randomSound >= 0) && (randomSound <= 3)) {screamSound = scream1;}
     if ((randomSound > 3) && (randomSound <= 6)) {screamSound = scream2;}
     if ((randomSound > 6) && (randomSound <= 10)) {screamSound = scream3;}    
 
     audio.PlayOneShot(screamSound);
 }
 
 // ----



for pausing or playing a sound, I have exactly the same problem, but these commands work when you attach the component audio source and place the sound in the audio clip box :

audio.volume = 0.0; audio.mute = true;

audio.volume = 1.0; audio.mute = false;

apart from that , I hope you get an answer so we both find out how to play / pause audioclip audio.Play();

:)

Comment
Add comment · Show 2 · 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 aldonaletto · Jan 26, 2012 at 07:15 PM 1
Share

@alucardj, you could do it more easier with a switch statement:

function playSound ()
{   
    randomSound = Random.Range(0,3);
    switch (randomSound){
      case 0: screamSound = scream1; break;
      case 1: screamSound = scream2; break;
      case 2: screamSound = scream3; break;
    }
    audio.PlayOneShot(screamSound);
}
Or even easier yet using an array:

var screams: AudioClip[]; // set size and fill it in the Inspector

function playSound () {
audio.PlayOneShot(screams[Random.Range(0, screams.length)]); }

avatar image AlucardJay · Jan 26, 2012 at 07:36 PM 0
Share

ahh, thanku very much. I came to unity from flash, and one command i was missing the most was switch - case. But the array method is the most elegant. (my bad random range scale was a leftover from before i realized Random.Range(0.0, 2.0) gave float , rr(0, 2) gave int. I should've fixed that too) $$anonymous$$any thanks.

avatar image
0

Answer by sjurick · Feb 28, 2014 at 03:53 PM

Help please. I have implemented the array solution as exampled above. I have an audio listener attached to the same prefab that the audio script is attached too with the array. I have two audio clips assigned to the array via the inspector. The only time audio works is when I actually assign an audio clip to the audio source, then, that's the only audio that gets played, not a random mix of the two.

 using UnityEngine;
 using System.Collections;
 
 public class Target : MonoBehaviour {
 
     public AudioClip[] audioClips;
 
     void Start () { }
 
     void Update () { }
     
     public void TargetHit ()
     {
         Debug.Log("Made it to Target.cs script");
         audio.PlayOneShot(audioClips[Random.Range(0,audioClips.Length)]);
         Destroy(this.gameObject);
         //Destroy(GameObject.Find("Target(Clone)"));
     }
 }
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

7 People are following this question.

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

Related Questions

Calculating rhythm of any music? 3 Answers

How To Turn Off Sound For Certain Objects? 1 Answer

unity won't play sounds i implement 1 Answer

Audio not coming out of speakers 1 Answer

How do I play a sound when pressing the W key, and when I stop pressing the W key, the sound stops? 2 Answers


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