- Home /
Array index is out of range?
I have searched around a bit and found tons of awnsers but nothing that really helped me!
Here is my code:
var Health : int = 50;
var prefabBullet:Transform;
var shootForce:float;
var jump : GameObject;
var smoke : GameObject;
var smoking : int = 20;
var HealthPoint : Texture[];
var SpawnPoints : Transform[];
var Deaths : AudioClip[];
var Jump : AudioClip[];
function Update () {
if(Health <= 0){
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
death();
Health = 50;
}
if(Health <= smoking)
smoke.active = true;
if(Health >= smoking)
smoke.active = false;
if (Input.GetButtonDown("Jump")){
audio.clip = Jump[0];
audio.Play();
jump.active = true;
}
if (Input.GetButtonUp("Jump")){
audio.clip = Jump[0];
audio.Stop();
jump.active = false;
}
}
function OnCollisionEnter(collision : Collision){
if(collision.gameObject.tag == "Bullet"){
var randDamage = Random.Range(1, 5);
var damage = randDamage;
switch(damage){
case 1:
Health -=1;
break;
case 2:
Health -=2;
break;
case 3:
Health -=3;
break;
case 4:
Health -=4;
break;
case 5:
Health -=5;
break;
}
}
}
function OnTriggerEnter(other : Collider){
if(other.tag == "Health"){
Health += 1*Time.deltaTime;
if(Health >= 50){
Health = 50;
}
}
}
function OnGUI() {
if(Health <= 50){
GUI.DrawTexture(Rect(0.0f,0.0f,Screen.width,Screen.height), HealthPoint[5]);
}
if(Health <= 40){
GUI.DrawTexture(Rect(0.0f,0.0f,Screen.width,Screen.height), HealthPoint[4]);
}
if(Health <= 30){
GUI.DrawTexture(Rect(0.0f,0.0f,Screen.width,Screen.height), HealthPoint[3]);
}
if(Health <= 20){
GUI.DrawTexture(Rect(0.0f,0.0f,Screen.width,Screen.height), HealthPoint[2]);
}
if(Health <= 10){
GUI.DrawTexture(Rect(0.0f,0.0f,Screen.width,Screen.height), HealthPoint[1]);
}
if(Health <= 1){
GUI.DrawTexture(Rect(0.0f,0.0f,Screen.width,Screen.height), HealthPoint[0]);
}
}
function death(){
AudioSource.PlayClipAtPoint(Deaths[ Random.Range(0, Deaths.Length)], transform.position, 2.0f);
i = Random.Range(0, SpawnPoints.Length);
transform.position = SpawnPoints[i].position;
transform.rotation = SpawnPoints[i].rotation;
}
Error message: IndexOutOfRangeException: Array index is out of range. Player.OnGUI () (at Assets/Scripts/Player.js:80)
The most likely culprit is the HealthPoint[] array. It is expecting to have 6 textures, and most likely these textures need to be assigned in the inspector:
In the Hierarchy, select the game object this script is attached to.
Find the HelpPoint[] array in the inspector
Click on the triangle to the left of the name
There should be six initialized entries in this list. If there are not 6 or if any of them are empty, you need to drag new textures onto the empty slots.
If this is not the error, then I suggest you double click on the error in Unity and then view the line the error is on in $$anonymous$$ono. Then edit your question placing a comment on the line that has the error. The line numbers displayed above and the line number in Unity don't match, so it can be difficult to figure out the location of an error.
There is 6 textures, I can put what ever number I want in the inspector and it will give me this error! It's where I put the [5], I can put in 1-1000 there to and it says the same thing!
it draws the texture but lag out the game cuz of the error!
It not just the number, but all six slots have to be filled with textures that you dragged and dropped into the six slots. If all six slots are filled, then the next step is to place a comment in the code above so we can see which line is causing the error.
Answer by skyassasin22 · Mar 03, 2013 at 11:06 AM
Go to the gameobject where you attached this script, look in the inspector and look for the Health Point drop down and set the size the 6. I hope it helps.
nope, I can put in 10 there and it still gives me the error! I can put in 0-5 in the [5] as well and it still gives me that error!
I think it's really weird since it shows the right texture and all! It actually find the texture I have put in the slot that I want it to look for but still it says that it's out of range!
Answer by Ellis · Mar 03, 2013 at 03:49 PM
O gawd, never mid! I forgot that I had put this script on another object as well! xD