Array Index Out Range.. Can't figure out why.

Hi, I am very new to Unity, and Java Script as well, and I am trying to use the day / night cycle script which I got from here: http://forum.unity3d.com/threads/55093-Dynamic-Weather-Day-night-Cycle/page4 The latest version, seen near the bottom of that page, does not really function, so I am using an older version. This older version works correctly except for one part, which is causing a whole domino effect of errors… Here is the code. The error comes from line 46 (the return line in the CalculateColor function).

class PhasingColor
{
	var colors : Color[];
	var phaseRate : float;
	
	private var phaseTime : float = 0.0;
	private var phase : int = 0;
	
	function CalculateTime( worldTime : float ) : void
	{
		// Pre-note: Calculate time assumes that
		// "worldTime" is a floating point number in the range
		// [0-1).
		
		// The phase is determined by finding which chunk
		// of the phasing color the world time fits into.
		phase = worldTime * colors.length;
		
		// From there, we want to determine the 'sub-time'
		// by getting only the decimal part from the above
		// phase calculation.
		phaseTime = ( worldTime * colors.length ) - phase;
	}
	
	function IncrementTime() : void
	{
		phaseTime += Time.deltaTime;
		if( phaseTime > 1.0 )
		{
			phaseTime = 0.0;
			phase++;
			
		 if( phase >= colors.length ) { phase = 0; }
		}
	}
	
	
	 function CalculateColor() : Color
  {
    // Pre-calculate the color index to phase to.
    var newPhase : int;
    if( phase + 1 >= colors.length ) { newPhase =phase; }
    else{ newPhase = phase + 1; }

    // Do the Lerp.
    return Color.Lerp( colors[phase], colors[newPhase], phaseTime * 2 );
  }

}


var slider : float;
var sun : Light;

var dayBox : Material;
var noonBox : Material;
var nightBox : Material;

var mainLight : PhasingColor;
var ambientLight : PhasingColor;
var tints : PhasingColor;
var fog : PhasingColor;

private var hour : int;
private var minute : int;
private var curSkybox : Material;

function OnGUI()
{
	// Slider determines what time the scene is being viewed at.
	// In your own code, you can have the time calculation preceed
	// the lighting calculation, as the time seeds the light calculation.
	// That's best done from FixedUpdate().
	slider = GUI.HorizontalSlider( Rect( 20,20, 200,30 ), slider, 0, 1.0 );
	
	hour = slider * 24;
	minute = slider * 2.4;
	
	// Apply the slider calculation to the Sun object's transformation.
	// Then, make sure and adapt the slider value to agree with
	// the sun.
	
	sun.transform.localEulerAngles.x = ( slider * 360 ) - 90;
	slider = slider + Time.deltaTime / 300;
	if (slider > 1) {
slider = 0;
}
}

function FixedUpdate()
{
	// Now, make sure and tell all the Phasing Colors to update their
	// calculations based on this new time value!  When this code
	// has the GUI part stripped, you can instead use the new class
	// feature which is PhasingColor.IncrementTime().
	mainLight.CalculateTime( slider );
	ambientLight.CalculateTime( slider );
	tints.CalculateTime( slider );
	fog.CalculateTime( slider );
	
	// Ok, ok, let's just increment the time while we're at it.
	mainLight.IncrementTime();
	ambientLight.IncrementTime();
	tints.IncrementTime();
	fog.IncrementTime();
	
	// Now, we can apply all our colors at once.
	sun.color = mainLight.CalculateColor();
	RenderSettings.ambientLight = ambientLight.CalculateColor();
	RenderSettings.fogColor = fog.CalculateColor();
	
	// Now, before we apply the tint to the Skybox, we need to be
	// sure of which skybox we're even using.
if( slider < 0.33 ) { curSkybox = dayBox; }
else if( slider < 0.66 ) { curSkybox = noonBox; }
else { curSkybox = nightBox; }

// Now we can apply the tint to our temporary skybox, then push
// the skybox material onto the renderer.
curSkybox.SetColor( "_Tint", tints.CalculateColor() );
RenderSettings.skybox = curSkybox;
}

Any help with fixing this would be greatly appreciated! :slight_smile: Thanks!

~~Tofugames

By looking at your code, you declare the Color array, but I do see where you initialize (assign) values to this array.

var colors : Color[ ];

Therefore, you cannot access this array: return Color.Lerp( colors[phase], colors[newPhase], phaseTime * 2 );

Make sure that assign this array in your code or in the Inspector.