- Home /
IndexOutOfRangeException error for array points in range
I've created a Vector3 array with the length of four with this code (these are declared globally):
var contactpointSide : Vector3[] = new Vector3[4];
var contactnormalSide : Vector3[] = new Vector3[4];
and the array size in the inspector shows 4. Whenever I run the code, though, the size of the array changes to 0, and when I try to add vectors to the array, it gives me an IndexOutOfRangeException. Nowhere in the code do I modify or do anything related to the length of the vector, so I don't know why the vector size keeps changing to zero. Can anyone spot my problem or have any ideas?
Here's the code, with the offending lines marked (with unrelated stuff removed):
function OnCollisionStay (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
...
if(dotprod >= -0.5 && rotationprod > 0.7) // left
{
// stuff here
//THIS LINE//contactpointSide[0] = contact.point;
//THIS TOO// contactnormalSide[0] = contact.normal;
}
}
}
Did you make changes to the contactpointSide
and contactnormalSide
in any other part of your code?
Answer by Chronos-L · Apr 05, 2013 at 06:49 AM
#pragma strict
var contactpointSide : Vector3[] = new Vector3[4];
var contactnormalSide : Vector3[] = new Vector3[4];
function OnCollisionStay (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
// These 2 lines produce errors in your script
// because they have become null when
// this segment is executed
contactpointSide[0] = contact.point;
contactnormalSide[0] = contact.normal;
}
}
I ran this with the collider/rigidbody properly setup, and I can see contactpointsSide
and contactnormalSide
updates to new values when the rigidbody this script is attached to dropped onto and collide with a plane.
Okay, thanks for checking it out. Do you know why they have become null or how to code a workaround? I'm wanting to put different collision information into an array to pass to other functions.
why they have become null
You made them to (Whenever I run the code, though, the size of the array changes to 0), that's the only reason I can think of. I could be wrong, if anyone's opinion differs from $$anonymous$$e, speak up.
how to code a workaround
Don't make them become null. Post your script here (the whole script, if it is not possible, just keep codes that deal with the arrays. We will see if we can find out which line is causing the problem.
I actually don't change them via code at all, they just change by themselves whenever I collide with something. The lines in the code above are the only two places where I even have the contactpointSide or contactnormalSide variables mentioned, so I don't know why it's beco$$anonymous$$g null.
I'd be happy to post the code, but it's pretty long. Should I post it all here?
When I run the code in my answer (a very short version of yours), it works out alright, the only explanation I can think of is that your other code have changed the arrays.
Are you sure that you did not use/meddle with the arrays in your script? Do you $$anonymous$$d trying Search->Find-in-Files (Ctrl-Shift-F) on your project solution just to be sure?
Okay, I actually coded a work around. For some reason, whenever OnCollisionStay was called, it would reset the vector. I still don't know why, because I didn't modify the length in code, but I found out that declaring or redeclaring the length in Start kept the length when OnCollisionStay was called.
Thanks for the help, Chronos-L! I really appreciate the time you put into helping me out.
Your answer
Follow this Question
Related Questions
Index is Less Than Zero + Flawed Code 1 Answer
Array index is out of range 0 Answers
IndexOutofRangeException 1 Answer
my QandA array is not working when you choose 3 wrong answers 1 Answer
Array empties values on RunTime? 1 Answer