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
4
Question by PinkMustard · Aug 05, 2014 at 02:53 PM · audiosourceloadaudioclipresourcesnull

Resources.load returns null instead of AudioClip

Hello there! Im having a problem with my audioManager class. My problem is that the GetSoundEffect(effectToPlay) returns null for some reason, I have no idea why. The "Sound Emitter" object gets created successfully with an AudioSource component attached. I have tried to change the "as Audioclip" to a "typeof(AudioClip)" parameter in the return statement of the switch. That led to an error saying:

"Error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.AudioClip'. An explicit conversion exists (are you missing a cast?)"

What am I doing wrong here? Why doesn't any audio clip get loaded in to the AudioSource?

Any help is appreciated, thank you!

Here is a screenshot alt text

 public class audioManager {

         public enum SoundEffects{
                 block
         };
  
  
         public static void PlaySound(SoundEffects effectToPlay){  
                 //This returns null for some reason
                 GetAudioSource().clip = GetSoundEffect(effectToPlay);
                 //Nothing gets played
                 GetAudioSource().Play();  
         }//end PlaySound()
  
  
         //Create new sound emitter
         static GameObject soundObjectInstance = null;  
         static AudioSource GetAudioSource(){  
                 //if it doesnt excist, create it
                 if(soundObjectInstance == null){
                         soundObjectInstance = new GameObject("Sound Emitter");  
                         soundObjectInstance.AddComponent<AudioSource>();  
                 }  
                 return soundObjectInstance.GetComponent<AudioSource>();
         } //end GetAudioSource()
  
  
  
         static AudioClip GetSoundEffect(SoundEffects effectToPlay){
                 switch(effectToPlay){
                 case SoundEffects.block:
                         return Resources.Load("Sound/Block_Bounce.wav") as AudioClip;
                 break;
  
                 }
                 return null;
         }//end GetSoundEffect()
  
 }//end class


dump.jpg (42.0 kB)
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 WilliamLeu · Aug 05, 2014 at 04:05 PM 0
Share

When you specify the type as a parameter, you still have to cast. You also don't specify the extension.

 return Resources.Load("Sound/Block_Bounce", typeof(AudioClip)) as AudioClip;

And are you making sure that your path is relative to a parent folder named "Resources"? From the documentation:

Returns the asset at path if it can be found otherwise returns null. Only objects of type will be returned if this parameter is supplied. The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted.

1 Reply

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

Answer by Bunny83 · Aug 05, 2014 at 04:50 PM

You might want to read the docs again because it clearls said you have to omit the extension.

Also in general it's better to use one of the typed versions to prevent returning the wrong type of asset:

     return Resources.Load<AudioClip>("Sound/Block_Bounce");

or

     return (AudioClip)Resources.Load("Sound/Block_Bounce", typeof(AudioClip));

ps: When you have to use a cast, it's in general better to use the normal c-style cast instead of the as-cast. An as-cast will return null if the reference can't be casted into the given type while the c-style cast will throw an exception and tell you what went wrong.

ps: Hm i had this answer written about an hour ago, but hadn't submitted yet :D I just looked through my open tabs and found it again...

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 PinkMustard · Aug 06, 2014 at 01:33 PM 0
Share

Yes this worked perfectly! It must have been a cast error. I will read the documentation more carefully next time. Thank you so much!

avatar image TabuuForteAkugun · Dec 22, 2015 at 06:58 PM 0
Share

I have this problem as well, but even with your method, it still refuses to load the audioclip and keeps giving out null.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Resources.load returns null 1 Answer

Is it possible to load external audio files during a game? 0 Answers

Dynamically load AudioClip during runtime 1 Answer

Resources.Load wont read text file 6 Answers

Play sound at scene change time 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