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 francisliardet · Feb 12, 2015 at 10:49 AM · errorarray4.6

Index out of range exception

I realise this is probably a pretty common question, but I've looked at other answers and I really have no idea why these errors are coming up.

I have dragged four sounds into inspector to set up the array, and when I play the scene the ladder sounds work perfectly fine, as in it randomly chooses one of the four sounds and plays them like it should.

The error:

 IndexOutOfRangeException: Array index is out of range.
 WalkSoundScript+$PlayLadderSound$160+$.MoveNext() (at Assets/scripts /WalkSoundScript.js:83)

The Script in question:

 #pragma strict
 
 var sounds : AudioClip[];
 var ladderSounds : AudioClip[];
 
 var sneakSpeed : float = 0.04;
 var runSpeed : float = 0.06;
 var sneakDelay : float = 0.8;
 var runDelay : float = 0.35;
 var sneakVolume : float = 0.5;
 var runVolume : float = 1.0;
 var ladderDelay : float = 0.8;
 var ladderVolume : float = 0.2;
 var isGuard : boolean = false;
 
 private var lastPosition : Vector3 = Vector3.zero;
 private var isPlaying : boolean = false;
 private var isPlayingLadder : boolean = false;
 private var isJumping : boolean = false;
 
 private var speed : float = 0f;
 
 function Start () {
 
 }
 
 function Update () {
     if(speed > runSpeed){
         PlaySound(runDelay, runVolume);
     }
     else if(speed > sneakSpeed){
         PlaySound(sneakDelay, sneakVolume);
     }
     
     if(PlayerScript.onLadder && Mathf.Abs(Input.GetAxis("Vertical")) != 0 && !isGuard){
         PlayLadderSound();
     }
 }
  
 function FixedUpdate(){
     speed = (transform.position - lastPosition).magnitude;
     lastPosition = transform.position;
 }
 
 function PlaySound(delay : float, volume : float){
     if(isGuard){
         if(!isPlaying){
             audio.Stop();
             audio.volume = volume * 0.75;
             var num = Random.Range(0,3);
             audio.clip = sounds[num];
             isPlaying = true;
             audio.Play();
             
             yield WaitForSeconds(delay);
             isPlaying = false;
         }
     }else if(!isPlaying && PlayerAnimScript.grounded && !PlayerScript.onLadder){
         audio.Stop();
         audio.volume = volume;
         num = Random.Range(0,3);
         audio.clip = sounds[num];
         isPlaying = true;
         audio.Play();
         
         yield WaitForSeconds(delay);
         isPlaying = false;
     }
 }
 
 function PlayLadderSound(){
     if(!isPlayingLadder){
         
         audio.Stop();
         audio.volume = ladderVolume;
         var num = Random.Range(0,4);
         Debug.Log(num);
         audio.clip = ladderSounds[num];
         isPlayingLadder = true;
         audio.Play();
         
         yield WaitForSeconds(ladderDelay);
         isPlayingLadder = false;
     }
 }
 
 function PlayJump(){
     if(PlayerAnimScript.grounded && !isJumping){
             isJumping = true;
             AudioSource.PlayClipAtPoint(sounds[3], transform.position);
         
             yield WaitForSeconds(0.3);
             isJumping = false;
     }
 }


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

Answer by S_Darkwell · Mar 08, 2015 at 01:34 AM

The reason for your IndexOutOfRange exception is that Random.Range(0,4) will return one of the following: 0, 1, 2, 3, 4

The first sound in "ladderSounds" is actually at ladderSounds[0]. The last, if you add four sounds, is actually at ladderSounds[3].

The simple solution is to simply change:

 var num = Random.Range(0,4);

--in your PlayLadderSound() function to:

 var num = Random.Range(0,3);

A better solution, would be to replace that line instead with:

 var num = Random.Range(0,ladderSounds.Length - 1);

This will automatically generate a random number within the boundaries of your list.

Hope that helps!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

NullReferenceException error in an array of objects 0 Answers

Array index out of range error 1 Answer

Texture2d[]: Array index is out of rang (Javascript) 3 Answers

NullReferenceError, Tilemap Array with Transforms and Raycast (C# with Demo) 0 Answers

Check if no webcam 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