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 betaFlux · Dec 18, 2015 at 05:34 PM · nullreferenceexceptionresources.loadallaudioclips array

Array = null when passing as parameter

Straight to the point. The following code is in a script that executes before all other scripts:

 public static AudioClip[] FootstepSounds;
 
 void Start()
 {
     FootstepSounds = Resources.LoadAll("Sound Library/Footstep Sounds", typeof(AudioClip)) as AudioClip[];
 }

The above always worked for me and the path is correct. I then use this Array in another script like this:

 AudioClip currentAudioClip;
 void Footstep() // I'm using this as a Mecanim Event on a walk animation
 {
     Debug.Log("List null? -> " + SourceScript.FootstepSounds == null); // returns "List null? -> false"
     Debug.Log("List length -> " + SourceScript.FootstepSounds.Length); // NullReferenceException

     // of course NullReferenceException at the following line too
     currentAudioClip = SourceScript.FootstepSounds[Random.Range(0, SourceScript.FootstepSounds.Length)];
 }

The compiler tells me that the collection is not null, so it must be the elements, which were obviously provided inside the source script. Does anyone know what's going on?

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 OncaLupe · Dec 18, 2015 at 10:50 PM 0
Share

Are you positive the Start() is being run first? I'd suggest adding a Debug.Log() to confirm. I don't know $$anonymous$$ecanim, so if it's being triggered like an Awake(), it'll run before any Start() even if script order is set. You can move the Resources.LoadAll() to Awake and try it there as well.

Also, are you sure the Debug.Log in Footstep() is actually returning "List null? -> false"? When I run this line I get simply "False" due to order of operations. It's running the + operator first, then doing the equality check, so it's comparing a string to null. Try running it this way to confirm order of operations:

 Debug.Log("List null? -> " + (SourceScript.FootstepSounds == null) );

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by betaFlux · Dec 19, 2015 at 07:54 PM

Thanks for your suggestions, OncaLupe. It's weird but it looks like the only way to make it work, is to replace all 'AudioClip' occurrences with 'object' and cast to 'AudioClip' in the final assignment at line 8 in the second script.

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

33 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

Related Questions

Not sure why i'm getting NullReferenceException 0 Answers

NullReferenceException when changing Canvas text via code 0 Answers

NullReferenceException: Object reference not set to an instance of an object error ? 1 Answer

Help with my PopUp Damage Text! 0 Answers

A problem "NullReferenceException: Object reference not set to an instance of an object(...)" 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