Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lineweaver · Feb 29, 2020 at 03:45 PM · camerabuttonarrayindexoutofrangeexceptionescape

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 ^^

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lineweaver · Mar 01, 2020 at 02:22 PM 0
Share

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 ^^

avatar image Namey5 Lineweaver · Mar 01, 2020 at 11:34 PM 0
Share

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.

avatar image Lineweaver Namey5 · Mar 02, 2020 at 11:34 AM 0
Share

It works! Thank you for all your help ^^

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

212 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mysterious IndexOutOfRangeException 1 Answer

Index out of Range Exception Error 2 Answers

How to press button to change camera view? 0 Answers

How to reset the state of other buttons when clicking on one of them 0 Answers

Why does transform.rotate() do this? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges