How to deactivate then reactivate main camera

Hi, I have UI that I activate using JoystickButton7 through the following script:

using UnityEngine;
using System.Collections;

public class MenuToggle : MonoBehaviour
{
    private Canvas CanvasObject; // Assign in inspector

    void Start()
    {
        CanvasObject = GetComponent<Canvas>();
    }

    void Update()
    {
        if (Input.GetKeyUp(KeyCode.JoystickButton7))
        {
            CanvasObject.enabled = !CanvasObject.enabled;
        }
    }
}

The problem is that the UI has a main camera and so I need to deactivate the Main Scene camera so the new UI camera can be viewed.

Then when I press the JoystickButton7 again the main camera should reactivate. As the UI and its camera are deactivated.

So I have been throwing stuff at a wall and hoping something sticks because I don’t know C# well enough to even try and solve this myself. And so I appreciate any help offered.

Many thanks

Oh, I have been messing around with something like this but I cant get it to function.

if(showUIcamera)
    {
        GameObject.FindGameObjectWithTag("MainCamera").active = false;
    }
    else
    {
        GameObject.FindGameObjectWithTag("MainCamera").active = true;
    }

I suspect the main problem you’re having is that most of the “Find” methods can’t find GameObjects that are inactive, so you wouldn’t be able to turn it back on. You should try storing a reference to the camera when you find it. For example:

GameObject mainCam;

void Start() {
mainCam = GameObject.FindGameObjectWithTag("MainCamera");
}

void Update() {
if (showUICamera)
{
mainCam.SetActive(false);
}
else
{
mainCam.SetActive(true);
}
}

I also noticed while typing that that you were trying “.active”, which doesn’t exist. You use .SetActive to change its active state, and either .activeSelf or .activeInHierarchy to read its state.

1 Like

Much appreciated, Ill give that a go after my backup as completed.

Does this need to be on an empty game object? Or the same as my UI toggle?

Could be anywhere

Thanks for the response, much appreciated.

As in the image below, I added the camera toggle script to the OptionsMenuActivator game object along with the above mentioned MenuManager/toggle script as above.

The main camera is vThirdPersonCamera.

Camera Toggle Script

using UnityEngine;
using System.Collections;

public class CameraToggle : MonoBehaviour
{
    GameObject mainCam;
    private bool showUICamera;

    private void Start()
    {
        mainCam = GameObject.FindGameObjectWithTag("MainCamera");
    }

    void Update()
    {
        if (showUICamera)
        {
            mainCam.SetActive(false);
        }
        else
        {
            mainCam.SetActive(true);
        }
    }
}

But that didnt work. Actually when I tried to manually deactivate the main camera it wouldnt let me?

Any ideas what I could be doing wrong?

Thanks

It does that because it is working. showUICamera is just always false. You need to add code to change that bool.

1 Like

The UICamera is within the OptionsMenu game object which gets activated when I press the JoystickButton7.

The UICamera was active but thee OptionMenu was not active until I press the JoystickButton7…

Not sure whats going on now or how to solve it?

As a new developer I am wondering how to change the camera using the 1 and 2 buttons on the keypad / number row
is it just to delete joystick button 7 and add key 1 / 2?