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 ShinyTaco · Oct 15, 2014 at 02:32 AM · soundplay

How to Play a Sound from an Array from an Overloaded Method?

I have

 public AudioClip[] toPlay;

We call:

 playMySound(toPlay);

The method:

 public void playMySound (AudioClip sound){
     for(int i = 0; i < soundSource.Length; i++) {
         soundSource[i].audio.clip = sound;
         soundSource[i].audio.Play();
     }
 }


I get the following errors:

error CS1502: The best overloaded method match for myAudioScript.PlaySound(UnityEngine.AudioClip)' has some invalid arguments** **error CS1503: Argument #1' cannot convert UnityEngine.AudioClip[]' expression to type UnityEngine.AudioClip'

If I have:

 public AudioClip song;

There aren't any errors and it works fine, but obviously I cannot have an array.

Please help me to understand this better.

What is the proper syntax for this?

Thank in advance.

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
Best Answer

Answer by Fmressel · Oct 15, 2014 at 03:18 AM

I think your problem is that you are calling playMySound with an AudioClip array when playMySound only takes a single AudioClip. This is why when you change "song" to be "public AudioClip" instead of "public AudioClip[]" it works.

So, in order to fix your code to work with an array, you need to do the following:

         public AudioClip[] toPlay;
         
     //Since toPlay is of type AudioClip array and the variable needed by playMySound is of type   
     //AudioClip you need to choose which AudioClip in your array you want to pass to playMySound  
     //you do this by the below method  
     playMySound(toPlay[0]);
     //The number can be any size as long as it corresponds to an object in your array and should never exceed the length of the array itself 
     //Also remember that arrays count up from 0 so the first object in your array will be number 0
         
         public void playMySound (AudioClip sound){
             for(int i = 0; i < soundSource.Length; i++) {
                 soundSource[i].audio.clip = sound;
                 soundSource[i].audio.Play();
             }
         }

More info on arrays can be found here:
http://docs.unity3d.com/ScriptReference/Array.html

Hope that helps! If this didn't answer your question, please let me know!

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 ShinyTaco · Oct 15, 2014 at 04:00 AM 0
Share

Yeah that's it, I totally get it now. Thanks for your excellent explanation and response.

avatar image Fmressel · Oct 15, 2014 at 05:18 AM 0
Share

Glad to help! :)

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

29 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

Related Questions

Can not play a disabled audio source persistent problem 0 Answers

loop animation while audio is playing 0 Answers

Cant repeat Sound 1 Answer

Play Animation if Sound Output 0 Answers

Sound Timing 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