Camera switching mechanism
As a part of my research project, I have been testing a camera switching mechanism.
There are two things that I have been able to notice so far with the code below. Before I applied this code, the Main Camera was shown by default. However, now the last camera is shown by default, and even when I disable the script, I am unsure as to how I will fix this issue. Additionally, the code below throws an ArrayIndexOutofRangeException at line 46 in cameras[currentCameraIndex - 1].enabled = false; inside the else clause.
Do any of you guys possibly know what is going on and how I can solve it?
Thank you so much!
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public Camera cameras;
private int currentCameraIndex;
// Use this for initialization
void Start()
{
currentCameraIndex = 0;
//Turn all cameras off, except the first default one
for (int i = 1; i < cameras.Length; i++)
{
cameras[i].enabled = false;
}
//If any cameras were added to the controller, enable the first one
if (cameras.Length > 0)
{
cameras[0].enabled = true;
Debug.Log("Camera with name: " + cameras[0].name + ", is now enabled");
}
}
// Update is called once per frame
void Update()
{
//If the c button is pressed, switch to the next camera
//Set the camera at the current index to inactive, and set the next one in the array to active
//When we reach the end of the camera array, move back to the beginning or the array.
if (Input.GetKeyDown(KeyCode.C))
{
currentCameraIndex++;
Debug.Log("C button has been pressed. Switching to the next camera");
if (currentCameraIndex < cameras.Length)
{
cameras[currentCameraIndex - 1].enabled = false;
cameras[currentCameraIndex].enabled = true;
Debug.Log("Camera with name: " + cameras[currentCameraIndex].name + ", is now enabled");
}
else
{
cameras[currentCameraIndex - 1].enabled = false;
currentCameraIndex = 0;
cameras[currentCameraIndex].enabled = true;
Debug.Log("Camera with name: " + cameras[currentCameraIndex].name + ", is now enabled");
}
}
}
}
c# unity3d graphics camera indexoutofrangeexception
|
show 3 more comments
As a part of my research project, I have been testing a camera switching mechanism.
There are two things that I have been able to notice so far with the code below. Before I applied this code, the Main Camera was shown by default. However, now the last camera is shown by default, and even when I disable the script, I am unsure as to how I will fix this issue. Additionally, the code below throws an ArrayIndexOutofRangeException at line 46 in cameras[currentCameraIndex - 1].enabled = false; inside the else clause.
Do any of you guys possibly know what is going on and how I can solve it?
Thank you so much!
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public Camera cameras;
private int currentCameraIndex;
// Use this for initialization
void Start()
{
currentCameraIndex = 0;
//Turn all cameras off, except the first default one
for (int i = 1; i < cameras.Length; i++)
{
cameras[i].enabled = false;
}
//If any cameras were added to the controller, enable the first one
if (cameras.Length > 0)
{
cameras[0].enabled = true;
Debug.Log("Camera with name: " + cameras[0].name + ", is now enabled");
}
}
// Update is called once per frame
void Update()
{
//If the c button is pressed, switch to the next camera
//Set the camera at the current index to inactive, and set the next one in the array to active
//When we reach the end of the camera array, move back to the beginning or the array.
if (Input.GetKeyDown(KeyCode.C))
{
currentCameraIndex++;
Debug.Log("C button has been pressed. Switching to the next camera");
if (currentCameraIndex < cameras.Length)
{
cameras[currentCameraIndex - 1].enabled = false;
cameras[currentCameraIndex].enabled = true;
Debug.Log("Camera with name: " + cameras[currentCameraIndex].name + ", is now enabled");
}
else
{
cameras[currentCameraIndex - 1].enabled = false;
currentCameraIndex = 0;
cameras[currentCameraIndex].enabled = true;
Debug.Log("Camera with name: " + cameras[currentCameraIndex].name + ", is now enabled");
}
}
}
}
c# unity3d graphics camera indexoutofrangeexception
CancurrentCameraIndex
be modified in other places apart from those two methods?
– ikerbera
yesterday
Do you modifycameras
array in runtime or somewhere in other code?
– trollingchar
yesterday
Is your main camera the first camera in that array? you should also add an if check in your update, to make sure you camera array is atleast 1 or more. if you have a camera array with a size 0, then cameraIndex 0 is out of range. It would be more helpful if you could show us your inspect that has this script, and make sure it is the only instance of this script in your scene.
– Eddge
yesterday
@ikerbera I do not believe so, as these are the only two methods in the file.
– I Debarshi
23 hours ago
@trollingchar no.
– I Debarshi
23 hours ago
|
show 3 more comments
As a part of my research project, I have been testing a camera switching mechanism.
There are two things that I have been able to notice so far with the code below. Before I applied this code, the Main Camera was shown by default. However, now the last camera is shown by default, and even when I disable the script, I am unsure as to how I will fix this issue. Additionally, the code below throws an ArrayIndexOutofRangeException at line 46 in cameras[currentCameraIndex - 1].enabled = false; inside the else clause.
Do any of you guys possibly know what is going on and how I can solve it?
Thank you so much!
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public Camera cameras;
private int currentCameraIndex;
// Use this for initialization
void Start()
{
currentCameraIndex = 0;
//Turn all cameras off, except the first default one
for (int i = 1; i < cameras.Length; i++)
{
cameras[i].enabled = false;
}
//If any cameras were added to the controller, enable the first one
if (cameras.Length > 0)
{
cameras[0].enabled = true;
Debug.Log("Camera with name: " + cameras[0].name + ", is now enabled");
}
}
// Update is called once per frame
void Update()
{
//If the c button is pressed, switch to the next camera
//Set the camera at the current index to inactive, and set the next one in the array to active
//When we reach the end of the camera array, move back to the beginning or the array.
if (Input.GetKeyDown(KeyCode.C))
{
currentCameraIndex++;
Debug.Log("C button has been pressed. Switching to the next camera");
if (currentCameraIndex < cameras.Length)
{
cameras[currentCameraIndex - 1].enabled = false;
cameras[currentCameraIndex].enabled = true;
Debug.Log("Camera with name: " + cameras[currentCameraIndex].name + ", is now enabled");
}
else
{
cameras[currentCameraIndex - 1].enabled = false;
currentCameraIndex = 0;
cameras[currentCameraIndex].enabled = true;
Debug.Log("Camera with name: " + cameras[currentCameraIndex].name + ", is now enabled");
}
}
}
}
c# unity3d graphics camera indexoutofrangeexception
As a part of my research project, I have been testing a camera switching mechanism.
There are two things that I have been able to notice so far with the code below. Before I applied this code, the Main Camera was shown by default. However, now the last camera is shown by default, and even when I disable the script, I am unsure as to how I will fix this issue. Additionally, the code below throws an ArrayIndexOutofRangeException at line 46 in cameras[currentCameraIndex - 1].enabled = false; inside the else clause.
Do any of you guys possibly know what is going on and how I can solve it?
Thank you so much!
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public Camera cameras;
private int currentCameraIndex;
// Use this for initialization
void Start()
{
currentCameraIndex = 0;
//Turn all cameras off, except the first default one
for (int i = 1; i < cameras.Length; i++)
{
cameras[i].enabled = false;
}
//If any cameras were added to the controller, enable the first one
if (cameras.Length > 0)
{
cameras[0].enabled = true;
Debug.Log("Camera with name: " + cameras[0].name + ", is now enabled");
}
}
// Update is called once per frame
void Update()
{
//If the c button is pressed, switch to the next camera
//Set the camera at the current index to inactive, and set the next one in the array to active
//When we reach the end of the camera array, move back to the beginning or the array.
if (Input.GetKeyDown(KeyCode.C))
{
currentCameraIndex++;
Debug.Log("C button has been pressed. Switching to the next camera");
if (currentCameraIndex < cameras.Length)
{
cameras[currentCameraIndex - 1].enabled = false;
cameras[currentCameraIndex].enabled = true;
Debug.Log("Camera with name: " + cameras[currentCameraIndex].name + ", is now enabled");
}
else
{
cameras[currentCameraIndex - 1].enabled = false;
currentCameraIndex = 0;
cameras[currentCameraIndex].enabled = true;
Debug.Log("Camera with name: " + cameras[currentCameraIndex].name + ", is now enabled");
}
}
}
}
c# unity3d graphics camera indexoutofrangeexception
c# unity3d graphics camera indexoutofrangeexception
asked yesterday
I Debarshi
13
13
CancurrentCameraIndex
be modified in other places apart from those two methods?
– ikerbera
yesterday
Do you modifycameras
array in runtime or somewhere in other code?
– trollingchar
yesterday
Is your main camera the first camera in that array? you should also add an if check in your update, to make sure you camera array is atleast 1 or more. if you have a camera array with a size 0, then cameraIndex 0 is out of range. It would be more helpful if you could show us your inspect that has this script, and make sure it is the only instance of this script in your scene.
– Eddge
yesterday
@ikerbera I do not believe so, as these are the only two methods in the file.
– I Debarshi
23 hours ago
@trollingchar no.
– I Debarshi
23 hours ago
|
show 3 more comments
CancurrentCameraIndex
be modified in other places apart from those two methods?
– ikerbera
yesterday
Do you modifycameras
array in runtime or somewhere in other code?
– trollingchar
yesterday
Is your main camera the first camera in that array? you should also add an if check in your update, to make sure you camera array is atleast 1 or more. if you have a camera array with a size 0, then cameraIndex 0 is out of range. It would be more helpful if you could show us your inspect that has this script, and make sure it is the only instance of this script in your scene.
– Eddge
yesterday
@ikerbera I do not believe so, as these are the only two methods in the file.
– I Debarshi
23 hours ago
@trollingchar no.
– I Debarshi
23 hours ago
Can
currentCameraIndex
be modified in other places apart from those two methods?– ikerbera
yesterday
Can
currentCameraIndex
be modified in other places apart from those two methods?– ikerbera
yesterday
Do you modify
cameras
array in runtime or somewhere in other code?– trollingchar
yesterday
Do you modify
cameras
array in runtime or somewhere in other code?– trollingchar
yesterday
Is your main camera the first camera in that array? you should also add an if check in your update, to make sure you camera array is atleast 1 or more. if you have a camera array with a size 0, then cameraIndex 0 is out of range. It would be more helpful if you could show us your inspect that has this script, and make sure it is the only instance of this script in your scene.
– Eddge
yesterday
Is your main camera the first camera in that array? you should also add an if check in your update, to make sure you camera array is atleast 1 or more. if you have a camera array with a size 0, then cameraIndex 0 is out of range. It would be more helpful if you could show us your inspect that has this script, and make sure it is the only instance of this script in your scene.
– Eddge
yesterday
@ikerbera I do not believe so, as these are the only two methods in the file.
– I Debarshi
23 hours ago
@ikerbera I do not believe so, as these are the only two methods in the file.
– I Debarshi
23 hours ago
@trollingchar no.
– I Debarshi
23 hours ago
@trollingchar no.
– I Debarshi
23 hours ago
|
show 3 more comments
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53944187%2fcamera-switching-mechanism%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53944187%2fcamera-switching-mechanism%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can
currentCameraIndex
be modified in other places apart from those two methods?– ikerbera
yesterday
Do you modify
cameras
array in runtime or somewhere in other code?– trollingchar
yesterday
Is your main camera the first camera in that array? you should also add an if check in your update, to make sure you camera array is atleast 1 or more. if you have a camera array with a size 0, then cameraIndex 0 is out of range. It would be more helpful if you could show us your inspect that has this script, and make sure it is the only instance of this script in your scene.
– Eddge
yesterday
@ikerbera I do not believe so, as these are the only two methods in the file.
– I Debarshi
23 hours ago
@trollingchar no.
– I Debarshi
23 hours ago