- Home /
Question by
mattstephenson64 · Aug 17, 2013 at 01:35 AM ·
error-handling
IndexOutOfRangeException: How to FIX it?
So, I'm following this tutorial series on YouTube and I have gotten stuck! Here is the error message:
IndexOutOfRangeException: Array index is out of range. (wrapper stelemref) object:stelemref (object,intptr,object) gun.Start () (at Assets/gun.js:22)
And here is the code:
public enum gunType{ Automatic, semiAutomatic, Shotgun, BurstFire}
var spark : Transform;
var shotNumber : int;
var damage : float;
var fireRate : float;
var spread : float;
var recoil : float;
var weight : float;
var type : gunType;
var magSize : int;
var ammo : int;
private var mag : int;
private var nextFire = 0.0;
private var SparkArrays : Transform[];
function Start() {
SparkArrays = new Transform[shotNumber];
for(var i=0;1<SparkArrays.Length;i++) {
SparkArrays[i] = Instantiate(spark, spark.position, spark.rotation);
}
}
function Update () {
switch(type) {
case gunType.Automatic :
if(Input.GetButton("Fire1")&&Time.time > nextFire) {
nextFire = Time.time + fireRate;
FireA();
}
break;
case gunType.semiAutomatic :
if(Input.GetButtonUp("Fire1")&&Time.time > nextFire) {
nextFire = Time.time + fireRate;
FireA ();
}
break;
case gunType.semiAutomatic :
if(Input.GetButton("Fire1")&&Time.time > nextFire) {
nextFire = Time.time + fireRate;
FireS ();
}
break;
}
}
function FireS () {
for(var i=0;i<SparkArrays.Length;i++){
var hit : RaycastHit;
var BulletDirection = transform.TransformDirection(new Vector3(Random.Range(-spread,spread)*0.002,Random.Range(-spread,spread)*0.001,1));
if(Physics.Raycast(transform.position, BulletDirection, hit, Mathf.Infinity)) {
if(hit.transform.gameObject.tag=="Player")
hit.transform.SendMessage("M_damage", damage);
SparkArrays[i].position = hit.point;
}
}
mag--;
}
function FireA () {
var hit : RaycastHit;
var BulletDirection = transform.TransformDirection(new Vector3(Random.Range(-spread,spread)*0.002,Random.Range(-spread,spread)*0.001,1));
if(Physics.Raycast(transform.position, BulletDirection, hit, Mathf.Infinity)) {
if(hit.transform.gameObject.tag=="Player")
hit.transform.SendMessage("M_damage", damage);
spark.position = hit.point;
}
mag--;
}
Any help is appreciated!!!
Comment
Answer by robertbu · Aug 17, 2013 at 02:13 AM
Your problem is on this line:
for(var i=0;1<SparkArrays.Length;i++) {
If you look closly, you have a '1' where an 'i' should be. It should be:
for(var i=0; i<SparkArrays.Length; i++) {
Your answer
