Search Unity

Using buttons to change cameras

Discussion in 'Scripting' started by Larpushka, Oct 13, 2015.

  1. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    Hi guys, so I'm using the UI button onclick to change cameras. I'm doing it via simple script that I attach to a Manager empty gameobject. Buttons are named: Button 1, Button 2, Button 3.... Cameras are named Camera 1, Camera 2, Camera 3....
    Here is the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraChange : MonoBehaviour {
    5.     public Camera[] MyCameras; //attach in inspectors all scene cameras
    6.     public void CameraChanger () {
    7.         foreach (Camera ChosenCamera in MyCameras) {
    8.             if (gameObject.name [7] != ChosenCamera.name [7]) { //if the number of the button doesn't match the number of the camera
    9.                 ChosenCamera.GetComponent<Camera> ().enabled = false;
    10.             } else {
    11.                 ChosenCamera.GetComponent<Camera> ().enabled = true;
    12.  
    13.             }
    14.         }
    15.  
    16. }
    17. }
    18.  
    There is one problem with this code. gameObject.name[7] refers to the Manager object name and not to the button that is being pressed. Any ideas how to amend this?
     
  2. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    Hello! I would do this by passing along a function with each button, and also have that button pass in an int (from 0 to however many cameras you have). And use this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraChange : MonoBehaviour {
    5.  
    6.  
    7.     public Camera[] cameras;
    8.  
    9.     public void SelectCamera(int cam){
    10.         for (int i = 0; i < cameras.Length; i++) {
    11.             cameras[i].GetComponent<Camera>().enabled = false;
    12.         }
    13.  
    14.         cameras [cam].GetComponent<Camera> ().enabled = true;
    15.     }
    16.  
    17. }
    It will first disable all cameras, and then only enable the one with the int you've just passed in with button. So each button will have the same function, but pass through different ints.

    Hope that helps :)
     
  3. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    How can I make the button pass an int for cam? Don't I need another script for that and to create a public int cam that can be inherited?

    I'm thinking a much better way to handle it is to ditch the OnClick component and just IPointerDownHandler method. What do you think? Then I can use my if statement.
     
  4. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34


    Simple, just use the button component :)
    Where it says button you just drag in the gameobject that has the CameraChange script, then pass in the function and you can pass in an int (see the 1 in the image). Do this for each button and pass in an int for each button, from 0 to however many camerabuttons you have.
     
  5. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    It's not at all clear to me where you got the C# button script from. I thought the method with On Click would be shorter, but it's not. So instead I used this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class CameraChange : MonoBehaviour, IPointerDownHandler {
    7.     public Camera[] MyCameras;
    8.     // Use this for initialization
    9.  
    10.     public void OnPointerDown(PointerEventData eventData)
    11.     {
    12.     foreach (Camera ChosenCamera in MyCameras) {
    13.             if (gameObject.name [7] != ChosenCamera.name [7]) {
    14.                 ChosenCamera.GetComponent<Camera> ().enabled = false;
    15.             } else {
    16.                 ChosenCamera.GetComponent<Camera> ().enabled = true;
    17.     }
    18.             Debug.Log (gameObject.name);
    19.             Debug.Log (ChosenCamera);
    20.             }
    21.         }
    22.  
    23. }