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 Shamon91 · Sep 19, 2014 at 03:20 PM · c#arrayaudiobug

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

Hi there,

I am in the middle of finishing off a test environment that I am putting together as part of my Sound Design Portfolio. I have added some footstep sounds to the player by using the script that can be found in this youtube vid: https://www.youtube.com/watch?v=0nbNyJ7Vl-Y

I keep getting an "IndexOutOfRangeException: Array Index is out of range." error each time the player bumps into the wall that goes around the perimeter of the environment that I have made. The script works fine until that error occurs.

Can any of you chaps a Unity novice like me out? :)

I can paste the code here if you guys would prefer, but it has quite a lot of lines of code. But the C# script linked in the description of the YouTube vid that I pasted further up is pretty much untouched in my project.

Thanks :)

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 Graham-Dunnett · Sep 19, 2014 at 03:22 PM

Well, Index out of Range, means you are trying to access an array element that doesn't exist. If your array had 10 elements, and you tried to access the 11th element, you'd get this error. Also, if your array doesn't exist yet (because it's not been created) and you try and access it, you'll get the same error. Usually this means you ask how long the array is, get told zero, and try to access element zero. Further, the error message will include the exact line of code where the problem happens, so tracking down which array is causing the problems should be easy.

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
avatar image
0

Answer by dmg0600 · Sep 19, 2014 at 03:49 PM

If you have any tag setted so the sound for that tag can be played, you must have some AudioClips in the array for that certain tag.

This is because it tries to play the sound choosing the AudioClip from the ones in the array. If the array is null it selects a random number between 0 and the length of the array, which is 0. Therefore it tries to access arrayOfSounds[0] which is not correct as that array has no AudioClips.

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 Shamon91 · Sep 19, 2014 at 05:32 PM 0
Share

The script I'm using had several tags for surfaces for the player to walk on, "Concrete", "Dirt", "Floor", "Grass" etc. I am only using "Grass" (For the moment). I apologise, in my naivety, I have now tried gutting the script from it's original form so the only tag that remains is "Grass". But now I am getting compiler errors.

Here is the code:

Any advice on what I could do to tweak it would be appreciated :) I seriously need to learn C# more in depth.

 var grass: AudioClip [];
 private var step : boolean = true;
 var audioStepLengthWalk : float = 0.45;
 var audioStepLengthRun : float = 0.25;
 
 
 function OnControllerColliderHit (hit : ControllerColliderHit) {
 var controller : CharacterController = GetComponent(CharacterController);
 
 if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Grass"  && step == true || controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Untagged" && step == true ) {
         WalkOnGrass();
     } else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Grass" && step == true) {
         
 }
 ////////////////////////////////// GRASS ///////////////////////////////////////////////
 function WalkOnGrass() {    
     step = false;
     audio.clip = grass[Random.Range(0, grass.length)];
     audio.volume = .1;
     audio.Play();
     yield WaitForSeconds (audioStepLengthWalk);
     step = true;
 }
avatar image dmg0600 · Sep 19, 2014 at 05:41 PM 0
Share

You are missing a } after else if (...) { but as you don't even need the else, just remove it.

 var grass: AudioClip [];
 private var step : boolean = true;
 var audioStepLengthWalk : float = 0.45;
 var audioStepLengthRun : float = 0.25;
  
  
 function OnControllerColliderHit (hit : ControllerColliderHit) {
 var controller : CharacterController = GetComponent(CharacterController);
  
 if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Grass"  && step == true || controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Untagged" && step == true ) {
         WalkOnGrass();
     }
  
 }
 ////////////////////////////////// GRASS ///////////////////////////////////////////////
 function WalkOnGrass() {    
     step = false;
     audio.clip = grass[Random.Range(0, grass.length)];
     audio.volume = .1;
     audio.Play();
     yield WaitForSeconds (audioStepLengthWalk);
     step = true;
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem with World Generate 1 Answer

[SOLVED] Problem with "foreach". 1 Answer

Multiple Cars not working 1 Answer

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

Distribute terrain in zones 3 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