- Home /
IndexOutOfRangeException: Array index is out of range
IndexOutOfRangeException: Array index is out of range.
Shield.Effect (RaycastHit ray) (at Assets/Scripts/Ships/Shield.js:17)
UnityEngine.GameObject:SendMessage(String, Object)
LaserRed:FixedUpdate() (at Assets/Scripts/Ships/Lasers/LaserRed.js:18)
I get that when my shield powers down and stops making effects, its designed to sort through different effects based on strength, however when it powers down and shrinks then it gives me this. Its weird
shield script
var maxhealth : float;
var healrate : float;
var breaktime : float;
var se = new GameObject[4];
private var health : float;
private var healing : boolean = false;
function Start(){health = maxhealth;}
function Effect(ray : RaycastHit){
var hp = (health/maxhealth*100);
var rot = Quaternion.FromToRotation(Vector3.right, ray.normal);
if (hp <= 100 && hp >= 70){Instantiate(se[1],ray.point,rot);}
else if (hp <= 70 && hp >= 50){Instantiate(se[2],ray.point,rot);}
else if (hp <= 50 && hp >= 25){Instantiate(se[3],ray.point,rot);}
else{Instantiate(se[4],ray.point,rot);}
}
function Damage(d : float){
health+=(-d);
checkHealth();
}
function Healmanager(){
healing = true;
while (healing && healrate != 0){
health+=healrate;
if (health >= maxhealth){ health = maxhealth; healing = false;}
else{
yield WaitForSeconds(1);}
}
}
function ShieldBreak(){
var s : Vector3 = transform.localScale;
transform.localScale = Vector3();
healing = false;
yield WaitForSeconds(breaktime);
health = 1;
transform.localScale = s;
if (healing == false){Healmanager();}
}
function checkHealth(){
if (health > maxhealth){health = maxhealth; return;}
if (health <= 0){ShieldBreak();}
else if (health < maxhealth && healing == false){Healmanager();}
}
Answer by Graham-Dunnett · Jul 13, 2011 at 10:00 AM
The error you get is on line 17 in your script. When I count through the lines I see:
else{Instantiate(se[4],ray.point,rot);}
This says that you are accessing the 5th element in the array, but you have declared the se array as having 4 elements. Arrays are zero indexed in Javascript, right?
Pro tip. The error messages should always tell you which line the problem occurred on. The error you posted says that you are indexing an array with a value that is wrong, and this occurs on line 17 of the script called Shield.js. From there it's trivial to find the problem.
Answer by awesomealex · May 25, 2012 at 05:01 PM
Hi guys !!!
I've got nearly the same error:
IndexOutOfRangeException: Array index is out of range. ConservePhysicScript.Start () (at Assets/Script/ConservePhysicScript.js:15)
here's my code (I'm a 3D artist and that's the first time I'm makinf .js so sorry if it's look likes horrible for ypu to read ...). The goal here is to instantiate on the position of an object to an other one.
Any idea will be welcome!!!
var container : GameObject;
var MeshTransform:GameObject[];
var IsConservePlieeBase=false;
var conservePliee: GameObject;
var conserveFermee: GameObject;
function Start () {
container = GameObject.Find("conserve_container_prefab");
conserveFermee = GameObject.Find("conserve_fermee_base");
conservePliee = GameObject.Find("conserve_pliee_base");
MeshTransform[0].renderer.enabled = true;
MeshTransform[1].renderer.enabled = false;
}
function makeDamage(damage:Vector3){
MeshTransform[0].renderer.enabled = false;
MeshTransform[1].renderer.enabled = true;
Instantiate (MeshTransform[0], transform.position,transform.rotation);
conservePliee.rigidbody.AddForce(25*damage);
print("+50 PTS");
Destroy(conserveFermee);
//print(MeshTransform[1].renderer.enabled);
yield WaitForSeconds(5);
Destroy(conservePliee);
}
Your answer
Follow this Question
Related Questions
C# array not behaving as expected 0 Answers
Array index is out of range Error, only when array is used to Instantiate 4 Answers
NullReferenceException array 2 Answers
array problem 1 Answer