- Home /
Question by
pavster · Jul 18, 2013 at 09:19 AM ·
slenderslenderman
Array index is out of range
Hi When I always play the game it comes up with Array index is out of range? Can someone please help me?
#pragma strict
@script RequireComponent( AudioSource )
var papers : int = 0; // counter of how many papers collected
var papersToWin : int = 8; // total number of papers in the scene
var distanceToPaper : float = 5.5; // maximum distance that the raycast will detect
var sphereRadius : float = 1.0; // the width of the sphere that is being SphereCast
var paperSound : AudioClip;
var musicClips : AudioClip[];
var enemyScript : NPCMovement;
function Start()
{
if( musicClips.Length < 4 )
{
Debug.LogWarning( "Need 4 Music Tracks in the Inspector" );
}
audio.loop = true;
audio.clip = musicClips[ 0 ];
audio.Play();
if ( !enemyScript )
{
Debug.LogWarning( "No Enemy Script in the Inspector" );
}
}
function Update()
{
if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown( KeyCode.E ) )
{
var hit : RaycastHit;
var rayOrigin : Ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0 ) );
if ( Physics.SphereCast( rayOrigin, sphereRadius, hit, distanceToPaper ) )
{
//Debug.Log( "SphereCast Hit : " + hit.collider.gameObject.name );
//Debug.DrawLine( Camera.main.transform.position, hit.point, Color.red, 1.5 );
if ( hit.collider.gameObject.name == "Paper" )
{
//Debug.Log( "SPHERE hit Paper for sure" );
papers += 1;
// play paper has been picked up sound
audio.PlayClipAtPoint( paperSound, hit.point );
// destroy the paper
Destroy( hit.collider.gameObject );
// tell enemy to follow closer
enemyScript.ReduceDistance();
// change the music based on the number of papers collected
if ( papers == 2 )
{
audio.Stop();
audio.clip = musicClips[ 1 ];
audio.Play();
}
else if ( papers == 4 )
{
audio.Stop();
audio.clip = musicClips[ 2 ];
audio.Play();
}
else if ( papers == 6 )
{
audio.Stop();
audio.clip = musicClips[ 3 ];
audio.Play();
}
else if ( papers == papersToWin )
{
Debug.Log( "You have collected All Papers !" );
// load Win Scene here !!!!
}
}
}
}
}
Comment
Answer by ragnaros100 · Jul 18, 2013 at 09:36 AM
In the lines adio.clip = musicClips[x]
put the x-values one unit lower and see if the error still pops up.
Arrays start from 0 you see. (0, 1, 2...) and in unitys inspector view you can set the legth of elements (let's take 3) that the array can have. But as said, arrays start with 0.
Thit confuses a lot of new coders.
Hope this helps
Your answer
Follow this Question
Related Questions
Music Wont Stop From Page Script+ Extra Problom 1 Answer
What happened to the Slender Guide by alucardj 1 Answer
Ambient Script CS0163 1 Answer
Slender - Collecting papers with left mouse click? 1 Answer
Audio Wont Play In Page Script 0 Answers