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 !!!!
}
}
}
}
}