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 Chappy · Apr 10, 2014 at 12:44 AM · c#arrayaudio

Playing a soundclip at random from an array. C#

I've never really worked with audio coding before but I need to play a sound clip at random from a group of 5 whenever the player hits an object. Right now I've just got it in some test code and once I've got things figured out I'll trasplant it into the correct code.

 public AudioSource[] DamageClips;
 public GameObject Thief;
 
 void Start () 
     {
 
         Thief = GameObject.FindGameObjectWithTag ("Thief");
 
         AudioClip nextClip = Random.Range (0, DamageClips.Length);
 
         audio.clip = nextClip;
 
 
 void TouchHasBegun()
     {
         Ray ray = Camera.main.ScreenPointToRay( screenPos );
         RaycastHit hit;
         
         if ( Physics.Raycast( ray, out hit ) )
         {
             hit.collider.gameObject.SendMessage("OnClick", SendMessageOptions.DontRequireReceiver);
             //Debug.Log("PAUSE GODDAMN IT");
         }
         
         //if (falling == false && levelStart == true && jumpHasPlayed == false)
         //{
             //this.rigidbody.useGravity = true;
 
             //levelStart = false;
             //falling = true;
             //Camera.main.backgroundColor = Color.blue;
             
         //}
         
 
         else if(camTransform.position.y <= 170.0f)
         {
             anchor.rigidbody.constraints = RigidbodyConstraints.FreezeAll;    
             Debug.Log("STOP");
             Thief.animation.Stop("Falling");
             Thief.animation.Play("Grabbing");
             isFallingPlaying = false;
             audio.Play(DamageClips[Random.Range (0,DamageClips.Length)]);
         }


I've looked through some other answers, which is where I got what I have here but I'm getting a few errors.

Cannot implicitly convert type int' to UnityEngine.AudioClip'

The best overloaded method match for UnityEngine.AudioSource.Play(ulong)' has some invalid arguments Argument #1' cannot convert UnityEngine.AudioSource' expression to type ulong'

To be quite honest, I have no idea what I'm doing here so I don't really know how to fix these. Any help would be greatly appreciated.

Also, I have taken chunks my code out to post what I have here, most of it has nothing to do with the audio.

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

Answer by getyour411 · Apr 10, 2014 at 12:49 AM

I couldn't help but envision Cartman when I read

//Debug.Log("PAUSE GODDAMN IT");

Take a look at this question/answer for a simple syntax example

http://answers.unity3d.com/questions/403748/array-of-audioclips-not-playing-please-help.html

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 Chappy · Apr 10, 2014 at 01:26 AM 0
Share

Yeah, I was working on getting a pause button working and making Debugs relieves some stress.

I was able to get rid of the errors but now the soundclips won't play.

I've got an audiosource on the object (do I need to make an audio source for every clip?) and an audiolistener on the camera, my computers volume is set to max, the audiosource is within the $$anonymous$$imum range of the audiolistener and I've got the audiosource volume as high as it'll go. I've put the clips in the array and the rest of the touch code works so I know it's detecting it. Is there anything else I would need to do to make it work? I'm using the mobile unity remote app for touch control testing.

avatar image
0

Answer by FeedMyKids1 · Jun 17, 2020 at 01:10 AM

You need to change DamageClips to be AudioClips instead of AudioSources: public AudioSource[] DamageClips; to public AudioClip[] DamageClips;

Then, in 'Start' change this AudioClip nextClip = Random.Range (0, DamageClips.Length); to AudioClip nextClip =DamageClips[ Random.Range (0, DamageClips.Length)];

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

22 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

Related Questions

Can't get sound clip from array to play properly 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Using a footstep C# script and keep getting an "IndexOutOfRangeException" 2 Answers

Make an array of scripts? 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