2
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class cameraswitch : MonoBehaviour {
 public Camera cam1;
 public Camera cam2;



void Start () {
    cam1.enabled = true;
    cam2.enabled = false;
    Button Button = gameObject.GetComponent<Button>();
    Button.onClick.AddListener(baz);
}


void baz () {
    cam1.enabled = false;
    cam2.enabled = true;
}
}

this is my code . how to switch between the cameras with one button . and Thank You.

\$\endgroup\$
2
  • \$\begingroup\$ Have you read up on Coroutines in Unity? \$\endgroup\$ – DMGregory Jun 4 '18 at 18:40
  • 1
    \$\begingroup\$ I see you've edited your question to change the content. Can you clarify whether your current code is successfully "switching between cameras with one button"? If not, in what way does it fail? \$\endgroup\$ – DMGregory Jun 4 '18 at 19:04
2
\$\begingroup\$

If I didn't understand you wrong, inside baz function you should do this:

cam1.enabled = !cam1.enabled;
cam2.enabled = !cam2.enabled;

This way you reverse the previous state of the cameras.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.