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 /
avatar image
0
Question by tas41 · Mar 11, 2017 at 10:05 AM · c#functionarraysresources.loadalgorithm

Algorithm for loading sounds

So I wrote this function to load in a bunch of step sounds in an array of different step materials, inside each step material is a sneak step, walk step and run step sound.

So lets say in one instance

  • i ,in the first loop for materials, is 3, which is metal

  • t , in the second loop for step type is 1, which is sneak

  • r, in one of the third loops is 4, which is the 4th clip

  • The filename would be step_sneak_metal4 , and it is if you use debug.logs.

And the "Resources.Load("filename") as AudioClip " works everywhere else. and again works according to debug.log and says this is a valid file that exists within the resource folder.

but if you set it equal to steps[i].run[r] or steps[i].sneak[r] or whatever it says "Null Reference Exception: Object Reference not set to an instance of an object" but not outside of the function with a similar assignment.

   public void load()
     {
         steps = new StepPack[stepTypes.Length];
         for (int i = 0; i < stepTypes.Length; i++)
         {
             for (int t = 0; t < 3; t++)
             {
                 switch (t)
                 {
                     case 0:
                         for (int r = 0; r < 5; r++) steps[i].run[r] = Resources.Load("SFX/player/step/step_run_" + stepTypes[i] + (r + 1).ToString()) as AudioClip; 
                         break;
                     case 1:
                         for (int r = 0; r < 5; r++) steps[i].sneak[r] = Resources.Load("SFX/player/step/step_sneak_" + stepTypes[i] + (r + 1).ToString()) as AudioClip;
                         break;
                     case 2:
                         for (int r = 0; r < 5; r++) steps[i].walk[r] = Resources.Load("SFX/player/step/step_walk_" + stepTypes[i] + (r + 1).ToString()) as AudioClip;
                         break;
                 }
             }
         }
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 Bunny83 · Mar 11, 2017 at 01:07 PM

What is "StepPack"? I guess it's a custom class. You never create any instances of that class.

This:

 steps = new StepPack[stepTypes.Length];

only creates an array but each element in that array defaults to "null". Further more you have to ensure that your nested arrays (run, sneak, walk) are initialized as well inside the StepPack class.

Also using a switch case like this makes no sense and is bad practise. Each loop iteration only one case will be processed. So you can just scrap that whole for loop and just execute those 3 steps one after another.

So something like this:

 steps = new StepPack[stepTypes.Length];
 for (int i = 0; i < stepTypes.Length; i++)
 {
     // creating an actual instance of your StepPack class
     steps[i] = new StepPack();
     
     string path = "SFX/player/step/step_";
     string type = stepTypes[i];
     for (int r = 0; r < 5; r++)
     {
         steps[i].run[r] = Resources.Load<AudioClip>(path+"run_" + type + (r + 1)); 
         steps[i].sneak[r] = Resources.Load<AudioClip>(path+"sneak_" + type + (r + 1));
         steps[i].walk[r] = Resources.Load<AudioClip>(path+"walk_" + type + (r + 1));
     }
 }

You may want to use a format string instead and use string.Format to replace the different parts.

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 tas41 · Mar 11, 2017 at 07:46 PM 0
Share

Ah yes, forgive me for my sloppiness and omission of important items, but thank you for your answer!

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Arrays and indices issues 1 Answer

Check times a value is next to the same value in an array 4 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to load Audioclip using resources.load 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