- Home /
Array breaking when pushing floats into it via a for loop.
so I'm trying to figure out a chunk of code that is supposed to grab a group of objects by tag, and then create a an array of material colors to re-assign to their respective materials as a pulsing animation. the code was designed this way so any object tagged "pulse" would be capable of pulsing without any other changes made to it.
I get a clean count of 95 when I run the code, so I know it's finding the objects and adding them to the first array. it's when it tries to add the material colors to the second array when it breaks. I cannot find any examples of this error code online for js.
IndexOutOfRangeException: index
System.Array.System.Collections.IList.get_Item (Int32 index) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System/Array.cs:303)
Boo.Lang.Runtime.RuntimeServices.GetArraySlice (System.Object target, System.Object[] args)
Boo.Lang.Runtime.RuntimeServices.GetSlice (System.Object target, System.String name, System.Object[] args)
Boo.Lang.Runtime.DynamicDispatching.SliceDispatcherFactory+<CreateGetter>c__AnonStorey12.<>m__5 (System.Object o, System.Object[] arguments)
Boo.Lang.Runtime.RuntimeServices.GetSlice (System.Object target, System.String name, System.Object[] args)
Pulse.Awake () (at Assets/Scripts/Pulse.js:57)
the code seemingly causing the bug is below:
private var objects : Array ;
private var objColor : Array ;
private var matNum : int = 1 ; // for development: materials[matNum] ... most will use 1, simple objects will use 0
private var LightsCentral : LightsoutCentral ;
function Awake()
{
stageDurations = Array ( pulseTurnOnDuration, pulseStayOnDuration, pulseTurnOffDuration, pulseStayOffDuration ) ;
objects = GameObject.FindGameObjectsWithTag ("Pulse") ;
print(objects.length);
objColor = new Array() ;
if ( objects.length > 0 )
{
for ( var i = 0 ; i < objects.length ; i++ )
{
objColor.Push (objects[i].renderer.materials[matNum].color.a) ;
}
}
print(objColor);
LightsCentral = GameObject.Find ( "ScriptStorage" ) . GetComponent ( "LightsoutCentral" ) ;
}
any help identifying what needs fixed would be greatly appreciated!
JK
$$anonymous$$aybe you should use ArrayList(C#):
ArrayList a = new ArrayList();
a.Add(YOUR_THINGS_THAT_YOU_WANT);
You don't want to use ArrayList or JS Array. They are both slow, untyped, and obsolete. You should use a generic List ins$$anonymous$$d. (Not that this has anything to do with the code in the question.)
The top slot of an array in array length -1.
e.g. if an array has 2 slots, the slots are [0] and [1]
Shouldn't use unity build in Array class or ArrayList either, no design time type checking, Use a List or Collection.
In the case above put a break point in the for loop and step through your code, check objects[i] along with the material[matNum], because the for loop is based off the items in the objects Array, it shouldn't be throwing and exception, but matNum may be incorrect for the materials array. I would guess you are trying to pull a material from the materials array and it doesn't exist as an item by index.
Answer by Jeff-Kesselman · May 23, 2014 at 09:52 PM
This is your problem...
for ( var i = 0 ; i < objects.length ; i++ )
Array indices go from 0 to array length -1, so this should be
for ( var i = 0 ; i < objects.length-1 ; i++ )
Answer by JK3D · May 23, 2014 at 11:03 PM
so far, no dice with any of these... I tried switching the array to a list (for the list, used float, realized my mistake, switched to vector4, didn't work either, settled on color and it worked, kind of)
the error went from that long debug error to a short one saying I was miscasting, and once I got it casting properly.... back to the same big error again.
thanks anyhow everybody. at times like this I wish I was a programmer, not a designer.