- Home /
Instantiate into array : Out of range?
I'd like my enemies to explode into rigidbody pixels on death. The pixels work delightfully once they're instantiated, but I've hung myself up somehow on the instantiation command. I can't figure out why I get an "Array index is out of range" message. The array "points" contains the spots where these pixels will come into being.
var pixelPrefab : GameObject;
var points : GameObject[];
var pixels : GameObject[];
function OnTriggerEnter(other:Collider){
if (other.tag == "Killzone"){
for (var i=0;i<points.length;i++){
pixels[i] = Instantiate(pixelPrefab, points[i].transform.position, points[i].transform.rotation);
}
for (var hitMe : GameObject in pixels){
hitMe.rigidbody.AddExplosionForce(1000,transform.position,0);
}
Destroy(gameObject);
}
}
Where do you initialize pixels
? Does its length equal points.Length
at line 8?
Answer by meat5000 · Oct 07, 2013 at 04:14 PM
Have you tried a capital L? Do your points and pixels arrays match in length?
well he did write a small 'l' on array.length
but shouldn't that give him a compile error? not sure how he's asking about index out of range, since that code shouldn't even compile.
Yeah ain't sure about the capital. In the docs they interchange, but then the Array script reference shows several entries all called Array; not sure who wrote it :/
Anyway, as long as Pixels is not bigger than Points it should be fine.
Though the "l" doesn't seem to matter, I had overlooked the fact that I hadn't modified pixels' length from the default zero. Setting that to the same as points' righted everything. Thanks!