- Home /
Camera movement - index out of range
Im trying to make a simple escape room game, so I have 4 different rooms in one scene (basically 4 different sprites of a room with different objects on them), and I hve two buttons that change the camera position. The script looks like this:
public class CameraController : MonoBehaviour
{
public new Camera camera;
public Transform[] cameraPositions = new Transform[4];
private int roomNumber;
private void Start()
{
roomNumber = 0;
camera.transform.position = cameraPositions[roomNumber].position;
}
public void MoveCameraRight()
{
camera.transform.position = cameraPositions[roomNumber++].position;
Debug.Log(roomNumber);
}
public void MoveCameraLeft()
{
camera.transform.position = cameraPositions[roomNumber--].position;
Debug.Log(roomNumber);
}
}
I made 4 empty game objects that act like anchor points for the camera, and put them and the main camera onto the script. I put this script on an empty game object, and then put it on the buttons and gave each button a different method.
In essence, the script works. The thing is that the first click doesnt register, and when I reach the last room, I get an out of index range exception, and when I try to go back to the first room, the first clicks dont register again, and for some reason the number of all rooms goes down by one (for example, the first room, which is number 0, changes to -1). Is there any way Ican make this less glitchy? I've been told its possible to do this using a state machine but I'm not sure how to do it. Any help would be appreciated ^^
Answer by Namey5 · Mar 01, 2020 at 05:39 AM
The problem comes from the fact that every time you switch rooms, you increment/decrement the index without checking if it actually would lie within the array. When you try to access an element outside the boundaries of an array (in this case 0 - 3), we get undefined behaviour - the compiler doesn't know how to interpret this and nor should we.
Instead, if you want to limit your camera movement, you can check at the boundaries first, i.e.
public void MoveCameraRight()
{
if (roomNumber == cameraPositions.Length - 1)
return;
camera.transform.position = cameraPositions[roomNumber++].position;
Debug.Log(roomNumber);
}
public void MoveCameraLeft()
{
if (roomNumber == 0)
return;
camera.transform.position = cameraPositions[roomNumber--].position;
Debug.Log(roomNumber);
}
Similarly, if you want the camera to wrap around to the other side, you can do that as well;
public void MoveCameraRight()
{
roomNumber = (roomNumber + 1) % cameraPositions.Length;
camera.transform.position = cameraPositions[roomNumber].position;
Debug.Log(roomNumber);
}
public void MoveCameraLeft()
{
roomNumber = (roomNumber - 1 + cameraPositions.Length) % cameraPositions.Length;
camera.transform.position = cameraPositions[roomNumber].position;
Debug.Log(roomNumber);
}
The script where the camera wraps to the other side works, except for the part that if I press left on the first room, i get an index out of range exception, but I can fix it by placing an invisible panel on the button when the camera is on the first room, thank you for your help ^^
Sorry, that was my bad. The modulus operator works a bit differently with negative numbers. I've updated the answer so it should properly work now without needing to change anything.
Your answer
Follow this Question
Related Questions
Mysterious IndexOutOfRangeException 1 Answer
Why does transform.rotate() do this? 1 Answer
IndexOutOfRangeException 1 Answer
IndexOutOfRangeException: Array index is out of range. 2 Answers