Array index is out of range. How to solve?
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");
}
}
Answer by Statement · Nov 01, 2015 at 11:38 PM
Your lights array is too small. The code requires exactly 4 lights. Check the inspector - the array is most likely empty, 1, 2 or 3 lights. It requires 4.
Or change the code so it works with any amount of lights.
I've alredy tried to put 4 lights, that error is removed, but I don't see the lights on..but I see that the lights in the inspector change.. Why? In the error bar there is this other message: Object reference not set to an instance of an object.. It says this is line is wrong.. lights[currentLight].enabled = false;
With the unity 3 project i didn't have this problem..
Thank you for your support
Object reference not set to an instance of an object
Your array is missing at least one light. The array might be 4 in size, but at least one of the items in the array is null.
Are you sure the error is co$$anonymous$$g from that specific object? If you single click the error message, it should "ping" the object in heirarchy (It'll look like a yellow highlight that kind of pops out a little)
When the error happen at runtime, see if you still got all lights in the array (if one was destroyed, for example, it wouldn't work).
Also if you write to the array at runtime, make sure you write to it before your code access the array contents. Likewise, be wary of writing null values into the array at runtime.
Your answer
Follow this Question
Related Questions
Array index out of range. But it doesn't appear to be. 2 Answers
Object Pooling Spawn Script 1 Answer
Assign values to array elements 1 Answer
need help in randomising array 1 Answer
How to declare a list of arrays? 1 Answer