- Home /
camera.enabled = true not working?
I have a game where the play switches between many cameras. When the player presses an arrow key the script is suppose to disable all cameras and then enable the one the player wanted to go to. The problem is that the cameras are all staying kinda disabled. They don't become active during game play, so the player is just starting at a blank screen. But the code says the camera is enabled. I know this because I have it print the camera the player is on which is checked by the if(camB4.enabled == true) print(camB4.name); So what is going on?
The code is quite long and complex so I will just post where i think the problem lies and if you need more let me know. The code is in C#. This swaps the camera:
void changeCamera(Camera cam)
{
camA3.enabled = false;
camA4.enabled = false;
camB2.enabled = false;
camB3.enabled = false;
camB4.enabled = false;
camB5.enabled = false;
camC1.enabled = false;
camC2.enabled = false;
camC3.enabled = false;
camC4.enabled = false;
camC5.enabled = false;
camC6.enabled = false;
camD1.enabled = false;
camD2.enabled = false;
camD3.enabled = false;
camD4.enabled = false;
camD5.enabled = false;
camD6.enabled = false;
camE2.enabled = false;
camE3.enabled = false;
camE4.enabled = false;
camE5.enabled = false;
camF3.enabled = false;
camF4.enabled = false;
mainCam.enabled = false;
cam.enabled = true;
print("Your current camera is: " + cam.name);
}
And this is what happens when a player hits an arrow key. I am just showing one interlocked if statement. There are many.
void moveCamera(string direction)
{
//Changes current camera
if(camA3.enabled == true)
{
if(direction == up)
{
print("invalid move");
}
else if(direction == down)
{
if(A3B3 == false)
{
A3B3 = true;
changeCamera(camB3);
}
else
{
print("That wall has been used already");
}
}
else if(direction == left)
{
print("invalid move");
}
else if(direction == right)
{
if(A3A4 == false)
{
A3A4 = true;
changeCamera(camA4);
}
else
{
print("That wall has been used already");
}
}
}//END A3
}
Thanks for the help!
Do you need all these cameras? What I mean is if I want lots of camera positions I line up my camera, create a correctly orientated empty at this position, then do this for all my positions, then simply switch where the camera is placed. If you want these positions to move you have the empty move via parenting/code and when you switch the camera position you parent it to the empty and set rotation and position to be the same as the empty's. ???
The person asking me to write it wants all the cameras. But I figured out what I needed to do. I just changed all the cam.enables to cam.gameObject.SetActive(true);
As mentioned above, here is what it would look like with array:
[SerializeField] private Camera[]camArray;
private Camera activeCam;
void ChangeCamera(Camera cam)
{
foreach(Camera c in camArray){
c.enabled = false;
}
Camera c = Array.Find(camArray, p => p == cam);
c.enabled = true;
activeCam = c;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why isnt renderer.isVisible not working? 1 Answer
How can I display text so that it can be see on the Vive Headset? 0 Answers