Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by D-Coy · Nov 23, 2015 at 02:14 PM · arrayaudioclip

play sound from an array

Ive spent 24 hours going around in circles with this, and it seems like nothing is compatible, or items are depreciated and obsolete. I know there must be a way. I have an array, I can print random items from the array. I can also play an individual audio.clip. But as soon as I try to put it all together I get incompatibility errors.

Ive commented out lines that I have tried and failed on. Any help would be greatly appreciated.

 #pragma strict
 
 //var MetalHits: AudioClip;
 public var MetalHits:AudioClip[];
 //public var MetalHits:AudioClip;
 
 function Start() 
 {
     //MetalHits = GetComponent.<AudioSource>();
 }
 
 function OnCollisionEnter(collision: Collision) 
 {
     for (var contact: ContactPoint in collision.contacts) {
         Debug.DrawRay(contact.point, contact.normal, Color.white);
     }
     if (collision.relativeVelocity.magnitude > 2)
         //AudioSource.Play(MetalHits, transform.position);
 //         audio.PlayOneShot(MetalHits[1]);
         //audio.Play();
         
          //audio.PlayOneShot(MetalHits[Random.Range(0,MetalHits.Length)]);
         //        MetalHits.Play();
         Debug.Log("mag hit "+collision.relativeVelocity.magnitude);
 }
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
0
Best Answer

Answer by Statement · Nov 23, 2015 at 03:17 PM

 //var MetalHits: AudioClip;
 ...
 //MetalHits = GetComponent.<AudioSource>();

Looks like you've been trying to store the audio source component in an audio clip reference. That won't work. Store the audio source component in an audio source reference. I haven't tested the following, but perhaps it's closer to where you want to be?

 var MetalHits : AudioClip[];
 private var audioSource : AudioSource;
 
 audioSource = GetComponent.<AudioSource>();
 
 function OnCollisionEnter(collision : Collision) 
 {
     DrawCollisionNormals(collision);
     PlayCollisionSound(collision);
 }
 
 function DrawCollisionNormals(collision : Collision)
 {
      for (var contact : ContactPoint in collision.contacts)
          Debug.DrawRay(contact.point, contact.normal, Color.white);
 }
 
 function PlayCollisionSound(collision : Collision)
 {
     if (collision.relativeVelocity.magnitude > 2)
         PlayRandomClip(MetalHits);
 }
 
 function PlayRandomClip(clips : AudioClip[])
 {
     var clip = clips[Random.Range(0, clips.Length)];
     audioSource.PlayOneShot(clip);
 }
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
avatar image
0

Answer by D-Coy · Nov 23, 2015 at 03:54 PM

Thank you! Works perfectly! I had read that you could temporarily instantiate the audioSource and then play from it, but nothing worked... at least the way I tried. Thanks again, Ill play around with this so to get a better understanding.

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

37 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 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 I can play a sound in Array object when I clicking a button, But the Audioclip in different object? 0 Answers

Please help! Audio not working in another class.. 0 Answers

Drag and Drop into Custom Inspector ObjectField not Working 1 Answer

Play Audio Clips In Sequence From Array To Audio Sources In Array 0 Answers

How to make an editor extension to unity ui for switching audio clips? 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