0
\$\begingroup\$

I am having trouble with camera switching. I have two cameras: "Camera1" and "Camera2". A "button script" attached to Camera1 allows a switch to Camera2. It also positions the cameras.

There is a gameobject called "Main Menu" that is meant to be a background image for the button ; it is a GUI texture background (a flat jpeg image) .

At game start, clicking the button causes "Camera" components to be enabled and disabled appropriately, and it changes the game view background. So the button works (sort of!). But my problem is that the GUI texture does not disappear from game view, and nor does the button!

When I click the button, I want a complete change of game view, i.e., I want both the button and its GUI texture background to disappear from game view. But I am not getting that.

How can I make a GUI texture visible ONLY because it is in front of Camera1? How do I make the button visible ONLY when Camera1 is active?

Here is my button script:

      using UnityEngine;
      using System.Collections;
 public class PageButton : MonoBehaviour
 {
 public GameObject MainMenu ;
 public Camera cam1, cam2 ;
 void Start() {
 // position the Main Menu
     MainMenu = GameObject.Find("MainMenu"); 
     MainMenu.transform.position = new Vector3 (0, 0, 0);
 // Setup Camera1
     cam1 = GameObject.Find("Camera1" ).GetComponent<Camera>() as Camera;
     cam1.transform.position = new Vector3 (0, 0, -10);
     cam1.orthographic = true;
     cam1.orthographicSize = 5f;
     cam1.enabled = true;
 // Setup Camera2
     cam2 = GameObject.Find("Camera2" ).GetComponent<Camera>() as Camera;
     cam2.transform.position = new Vector3 (100, 100, 10);
     cam2.enabled = false;
     cam2.transform.Rotate(0,270,0, Space.World);
     cam2.orthographic = true;
     cam2.orthographicSize = 5f;
 }
 public void OnGUI()
 {  
     if(GUI.Button(new Rect(580,340,20,20), ">")) {
         Debug.Log ("Button pressed");
         cam1.enabled = false;
         cam2.enabled = true;
     }  
 } // end OnGUI()

 } // end PageButton
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

GUI is actually rendered according to the screen view port and not according to the camera. In other words it is camera independent.

You can try having a 3D text view or sprite in order to achieve the desired functionality. There are lot of other options also . Try Unity 5 GUI system or check out the NGUI plugin for unity. If you still want to use the current approach then you can switch off the GUI by checking whether a camera is active.

public void OnGUI()
 {  
     if(cam1.activeSelf)
     {
         if(GUI.Button(new Rect(580,340,20,20), ">")) {
            Debug.Log ("Button pressed");
            cam1.enabled = false;
            cam2.enabled = true;
         }  
     }
 } // end OnGUI()
\$\endgroup\$
3
  • \$\begingroup\$ OK Thanks. I did not realise that GUI was camera independent. That explains a lot. I tried your code ; my version of Unity (5.0.2) complained: "Type 'UnityEngine.Camera' does not contain a definition for 'activeSelf'". \$\endgroup\$
    – Pat Ward
    Commented Aug 30, 2015 at 23:23
  • \$\begingroup\$ "activeSelf" is for gameobject , so you can use it as cam.gameObject.activeSelf but not on the camera component , you can also use your own boolean to decide whether a particular gui element should be displayed. \$\endgroup\$
    – Hash Buoy
    Commented Aug 31, 2015 at 12:22
  • \$\begingroup\$ Thank you for a useful reply. Following your leads, I managed to get the functionality I wanted by adding this command to the OnGui() method, within the IF block: transform.position = cam1.ScreenToViewportPoint (new Vector3 (2000, 2000, -10)); \$\endgroup\$
    – Pat Ward
    Commented Aug 31, 2015 at 23:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .