Suppose I have 2 cameras in different positions. How to realize that when you press the button number 1 - camera number 1 is turned off and camera number 2 is active instead of it, and vice versa. When you press the button number 2 - the camera number 2 was turned off and the camera number 1 was active instead.

Ps As a bonus, it will be interesting if it is implemented not through buttons, but through a drag-and-drop slider.

  • through the buttons what? OnGUI or UI? or keyboard? or some other? - Alexey Shimansky
  • Interesting standard UI and NGUI. ps maybe I am not aware of all the concepts) - Artik Slayer

1 answer 1

The camera has an enabled property, by changing which you can turn the camera on or off. It remains only to apply this knowledge in the script.

If through the UI, then the buttons can assign an OnClick event.

Add two buttons:

enter image description here

In their properties we find OnClick .

enter image description here

In it, you need to select the object on which the script handler hangs, and then in the right part of this handler select the same method that will be processed.

enter image description here

Everything. Is done.

The script itself looks like this:

 using UnityEngine; public class SwitchCameras : MonoBehaviour { public Camera cam1; public Camera cam2; private void Start() { cam1.enabled = true; cam2.enabled = false; } public void Switch() { cam1.enabled = !cam1.enabled; cam2.enabled = !cam2.enabled; } } 

Where cam1 and cam2 assigned to cam1 and cam2 respectively. And where each of the cameras changes the enabled property to the opposite of what it had.


It is also possible to deal with cameras as normal objects, which can be made active or inactive via SetActive

that is, to turn on the camera you will need to write something like

 cam1.gameObject.SetActive(true); 

For example, in the same script we can define a method with the parameter

  public void Switch(int numCameraToOn) 

which will accept the number of the camera to be turned on. The rest - turn off.

When a method with arguments appears - in the button's OnClick event, you can add this parameter:

enter image description here

for one button, the argument is put for example the number 1, the other button - 2.

When one of the buttons is pressed, we turn on one camera, turn off the other through SetActive

  • And what if the buttons in one scene, and the camera in another? - Artik Slayer
  • @ArtikSlayer then actions with buttons are meaningless)) But in general, please explain what you have in mind ...... There are such kind of objects as singletons and DontDestroyOnLoad ..... they can be dragged from the stage to another scene, maybe it is you need to? - Alexey Shimansky
  • It's a shame) For example, I have a MainMenu scene, where there is a menu of options. In the options menu 2 buttons. (which was mentioned in the question). One includes 1 camera, the other 2 camera. But the cameras are in the Game scene. (In fact, the buttons should change the view from the top to the third person view). - Artik Slayer
  • one
    @ArtikSlayer you can change configs from the options menu, and when loading the Game scene, install the camera in the right place. At the moment when the options menu is displayed, Game most likely does not exist as an object in memory. - Nick Volynkin
  • Yeah, thanks. Already figured out how to do it. - Artik Slayer