Hi to all, I made this script with Unity 3 and It’s works fine… when I imported It in Unity 5 I get the error: Array index is out of range. How Can I solve this?
This script make change differents point lights in a certain Time… The console says that the errror is: lights[currentLight].enabled = false;
(line 75)
have a good day ^^
[System.Serializable]
public class AnimazioneLuci : MonoBehaviour {
// number of seconds between changing light
public float lightTime = 1;
// the current light that is lit.
int currentLight = 0;
public Light[] lights;
public KeyCode controlKey = KeyCode.X;
private bool on = false;
void Update(){
if (Input.GetKeyDown (controlKey)) {
on = !on;
if (!on) {
StartCoroutine ("AccendiLuci");
StopCoroutine ("SpegniLuci");
}
}
if (on) {
StartCoroutine("SpegniLuci");
StopCoroutine ("AccendiLuci");
}
}
IEnumerator AccendiLuci()
{
yield return new WaitForSeconds(lightTime);
Debug.Log("Changing light to number "+currentLight);
// disable the current light
lights[currentLight].enabled = false;
// change the index to the next light
currentLight++;
// if the index is 4 or more, go back to the first light
if (currentLight >= 4)
currentLight = 0;
// the current light is now the next light, so enable it.
lights[currentLight].enabled = true;
// call this method again to create an infinite loop.
StartCoroutine("AccendiLuci");
}
IEnumerator SpegniLuci()
{
yield return new WaitForSeconds(lightTime);
Debug.Log("Changing light to number "+currentLight);
// disable the current light
lights[currentLight].enabled = false;
// change the index to the next light
currentLight++;
// if the index is 4 or more, go back to the first light
if (currentLight >= 4)
currentLight = 0;
// the current light is now the next light, so enable it.
lights[currentLight].enabled = false;
// call this method again to create an infinite loop.
StartCoroutine("SpegniLuci");
}
}