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 KennyRose · Apr 28, 2013 at 10:46 PM · audioarraysaudiosourcerandom.rangeaudioplay

Looking for a method for implementing multiple random sound arrays.

I'm new to Unity and all this stuff and what I'm looking for is a concise tutorial (or script) for triggering random sound arrays for multiple objects. What I have is basically a Pong Style game with 3 balls and I want each ball to have a different array of sounds that it randomly chooses from when it collides with something. I have tried several methods and none of them seem to work. I have all the audio files stored in their own folders inside an an 'Audio' folder in my assets but nothing seems to work for me. I'm completely confused about the way arrays are supposed to work in Unity because everybody has a different method. Which one is simplest and works best? Thanks for your help.

Comment
Add comment · Show 1
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 whydoidoit · Apr 28, 2013 at 10:46 PM 0
Share

What language are you using?

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Matthew Scott · Apr 29, 2013 at 05:42 AM

 var audioClips : AudioClip[];
 
 function Start()
 {
 audio.PlayOneShot(audioClips[Random.Range(0, audioClips.Length)]);
 }

This is a javascript version of what I would do.

To populate your array with your audio clips, attach this to a gameobject, and then in the editor, look at the inspector panel and you will see the array appear there. Adjust the size value to open new slots in the array as needed and drag your audio clips in them.

Check these links in the docs;

http://docs.unity3d.com/Documentation/ScriptReference/Array.html http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html

Read up about Built-in ararys. (What I used the above example)

There fast and efficient and not hard to use once to get used to them.

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 KennyRose · Apr 29, 2013 at 06:33 PM 0
Share

I'll try this when I get home. I think my problem had something to do with that first line of code. I wasn't seeing the array in the inspector. $$anonymous$$aybe I missed it, but thanks for the advice. This looks like what I needed.

avatar image
1

Answer by KennyRose · Apr 29, 2013 at 11:00 PM

Yeah, I'm not getting access to the array in the inspector for some reason. Here is what I have so far.

var mainGameScript : MainGame;

var BallSparks : GameObject;

var audioClips : AudioClip[];

function Start ()

{

 audio.PlayOneShot (audioClips[Random.Range(0, audioClips.Length)]);
 

}

function Awake ()

{

 rigidbody.AddForce(6,0,6, ForceMode.Impulse);
 
 InvokeRepeating ("IncreaseBallVelocity",2,2);

}

function Update ()

{

 if (transform.position.z < -9) 
 
 {
 
     mainGameScript.GameOver();
 
     //Application.LoadLevel ("ComputerBluesMenu");
     
 }
 

}

function IncreaseBallVelocity()

{

 rigidbody.velocity *= 1.05;

 

}

function OnCollisionEnter (collision : Collision)

{

 Instantiate (BallSparks,transform.position, transform.rotation); 
 
     audio.Play();
 }

}

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 Matthew Scott · Apr 30, 2013 at 03:07 AM 0
Share

Hmm, you should do, are you absolutely sure your script is attached to a gameobject in your scene? Even an empty game object would do. When you click on the object in the hierarchy it's properties will show in the inspector panel, under where the script is it should absolutely be there.

Should look like this... alt text

Here i entered 3 as the size to open up 3 slots.

Double check this, if it's still not working you can assign them individually through code but it seems a little overkill because you can do it through the inspector.

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

14 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

Related Questions

Function Update play sound once 1 Answer

Audio Distortion effects? 0 Answers

Add Audio Source to a Prefab 2 Answers

2nd Audio Clip Not Playing 0 Answers

Need help with audio files causing unity to freeze 0 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