Search Unity

how to stop this error : IndexOutOfRangeException: Array index is out of range.

Discussion in 'Scripting' started by San_Holo, Nov 23, 2014.

  1. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    strangely it's all working, but I still see the error in my console and wondered if you could see why.

    I'm basically creating an array of two audio clips which I've assigned in the inspector and which play on command as instructed, but for some reason I see the error, the clips play as I said and I'm thinking is there a way to avoid this error?

    my script is below, have a gander...

    Code (csharp):
    1. public AudioClip[] warpSounds;
    I have two audio clips files in that and in Start ()

    Code (csharp):
    1. if (warpSounds!=null) audio.PlayOneShot (warpSounds [0]);
    It plays but that's the line with the error and later in Update I play the other sound once a button is pressed etc

    Code (csharp):
    1. if (playwarpsound) audio.PlayOneShot (warpSounds [1]);
    That's fine also, but I have that error and you would think it wouldn't play the audio file but it does, so I'm i trying to populate the array with something else first or what? please advise
     
  2. federicopintaluba

    federicopintaluba

    Joined:
    Oct 19, 2014
    Posts:
    13
    I suggest two things, if you know that in the entire game you will have only those 2 Audio Clips, just create two different variables for each one and forget about that error line.

    Another way is verifying that the actual audio clip is null, and not the entire array.

    Instead of:
    Code (CSharp):
    1. if (warpSounds!=null) audio.PlayOneShot (warpSounds [0]);
    Do:
    Code (CSharp):
    1. if (warpSounds[0]!=null) audio.PlayOneShot (warpSounds [0]);
    Or even better:
    Code (CSharp):
    1.  
    2. foreach(AudioClip c in warpSounds){
    3. if(c != null) {
    4. audio.PlayOneShot(c);
    5. }
    6. }
    7.  
    If something does not work let me know, I didn't try that code out.
     
    San_Holo likes this.
  3. mweldon

    mweldon

    Joined:
    Apr 19, 2010
    Posts:
    109
    That error would occur if warpSounds is an array of size 0. It is not null, but warpSounds[0] is. Check both:

    if( warpSounds != null && warpSounds[0] != null ) ...
     
    San_Holo likes this.
  4. dustinandrew

    dustinandrew

    Joined:
    Apr 30, 2010
    Posts:
    102
    I just upgraded a project from 4.5 to 4.6 and also see this error spammed in the editor:
    IndexOutOfRangeException: Array index is out of range.
    SyntaxTree.VisualStudio.Unity.Bridge.WinPath.Combine (System.String left, System.String right)
    SyntaxTree.VisualStudio.Unity.Bridge.UnityProject.Folders ()
    SyntaxTree.VisualStudio.Unity.Bridge.UnityProject+<>c__DisplayClass18.<Serialize>b__15 ()
    SyntaxTree.VisualStudio.Unity.Bridge.UnityProject.ItemGroup (System.IO.TextWriter w, System.Action a)
    SyntaxTree.VisualStudio.Unity.Bridge.UnityProject.Serialize (System.IO.TextWriter w)
    SyntaxTree.VisualStudio.Unity.Bridge.ProjectFile.WriteIfNecessary (SyntaxTree.VisualStudio.Unity.Bridge.FileGenerationHandler generationHandler)
    SyntaxTree.VisualStudio.Unity.Bridge.ProjectFilesGenerator.GenerateProjectFiles ()
    SyntaxTree.VisualStudio.Unity.Bridge.ProjectFilesGenerator+<>c__DisplayClass1.<.cctor>b__0 ()
    UnityEditor.EditorApplication.Internal_CallUpdateFunctions ()

    UPDATE:
    Looks like UnityVS needed to be reimported.
     
    Last edited: Dec 2, 2014
  5. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    Dustin, thanks for that tip. Indeed the problem was UnityVS (I just deleted it :) )
     
  6. Jamie_Rabbit_Choi

    Jamie_Rabbit_Choi

    Joined:
    Feb 8, 2014
    Posts:
    3
    Oh!! Dustin, thanks!!!