Question by
carsonsgaming · Oct 21, 2016 at 09:15 PM ·
errorarray-out-of-range-except
Array index is out of range?
I'm trying to use a first person controller for a survival game but I keep getting the error "IndexOutOfRangeException: Array index is out of range," I don't really know what this means but here's my code:
private void UpdateCameraPosition(float speed)
{
Vector3 newCameraPosition;
if (!m_UseHeadBob)
{
return;
}
if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
{
m_Camera.transform.localPosition =
m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
(speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
newCameraPosition = m_Camera.transform.localPosition;
newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
}
else
{
newCameraPosition = m_Camera.transform.localPosition;
newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
}
m_Camera.transform.localPosition = newCameraPosition;
}
Comment
The code is incomplete, accessing an array is not happening.
The error essentially means you're using an index for an array that's larger than its count (e.g. 2 enemies in an array and you're trying to access with index 2 or higher)
This also applies for accessing an array with indexer 0 if it's empty, since 0 is the first entry.
This may be because I didn't add the full array, I edited the post and added it.
Still can't see any array access. It usually uses square brackets.
Your answer
![](https://koobas.hobune.stream/wayback/20220612095139im_/https://answers.unity.com/themes/thub/images/avi.jpg)