Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

So Im Making A car Game And I wanna know who to make.....

Discussion in 'Scripting' started by Darkdragon0011, Jun 22, 2015.

  1. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    So Im Making A car Game And I wanna know who to make a camera switcher with one button please help i have a script that has four buttons for switching but i just want one Help!!!
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class NewBehaviour : MonoBehaviour {
    4.     public GameObject camera1;
    5.     public GameObject camera2;
    6.     public GameObject camera3;
    7.     public GameObject camera4;
    8.     public Texture texture;
    9.  
    10.     void Start() {
    11.         camera1.SetActive(true);
    12.         camera2.SetActive(false);
    13.         camera3.SetActive(false);
    14.         camera4.SetActive(false);      
    15.     }
    16.    
    17.     void OnGUI() {
    18.         if(GUI.Button(new Rect(0, 0, 100, 100), texture)) {
    19.             camera1.SetActive(true);
    20.             camera2.SetActive(false);
    21.             camera3.SetActive(false);
    22.             camera4.SetActive(false);
    23.         }
    24.         if(GUI.Button(new Rect(100, 0, 100, 100), texture)) {
    25.             camera1.SetActive(false);
    26.             camera2.SetActive(true);
    27.             camera3.SetActive(false);
    28.             camera4.SetActive(false);
    29.         }
    30.         if(GUI.Button(new Rect(200, 0, 100, 100), texture)) {
    31.             camera1.SetActive(false);
    32.             camera2.SetActive(false);
    33.             camera3.SetActive(true);
    34.             camera4.SetActive(false);
    35.         }
    36.         if(GUI.Button(new Rect(300, 0, 100, 100), texture)) {
    37.             camera1.SetActive(false);
    38.             camera2.SetActive(false);
    39.             camera3.SetActive(false);
    40.             camera4.SetActive(true);
    41.            
    42.         }
    43.     }
    44. }
    45.  
    Thanks

    Darkdragon
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Do you mean something like this?

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class NewBehaviour : MonoBehaviour
    4. {
    5.     public GameObject camera1;
    6.     public GameObject camera2;
    7.     public GameObject camera3;
    8.     public GameObject camera4;
    9.     public Texture texture;
    10.  
    11.     int currentView = 0;
    12.  
    13.     void Start()
    14.     {
    15.         camera1.SetActive(true);
    16.         camera2.SetActive(false);
    17.         camera3.SetActive(false);
    18.         camera4.SetActive(false);
    19.     }
    20.  
    21.     void OnGUI()
    22.     {
    23.         if (GUI.Button(new Rect(0, 0, 100, 100), texture))
    24.         {
    25.             switch (currentView)
    26.             {
    27.                 case 0:
    28.                     camera1.SetActive(true);
    29.                     camera2.SetActive(false);
    30.                     camera3.SetActive(false);
    31.                     camera4.SetActive(false);
    32.  
    33.                     break;
    34.                 case 1:
    35.                     camera1.SetActive(false);
    36.                     camera2.SetActive(true);
    37.                     camera3.SetActive(false);
    38.                     camera4.SetActive(false);
    39.  
    40.                     break;
    41.                 case 2:
    42.                     camera1.SetActive(false);
    43.                     camera2.SetActive(false);
    44.                     camera3.SetActive(true);
    45.                     camera4.SetActive(false);
    46.  
    47.                     break;
    48.                 case 3:
    49.                     camera1.SetActive(false);
    50.                     camera2.SetActive(false);
    51.                     camera3.SetActive(false);
    52.                     camera4.SetActive(true);
    53.  
    54.                     break;
    55.             }
    56.  
    57.             currentView = (currentView + 1) % 4;
    58.         }
    59.     }
    60. }
    61.  
     
  3. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    Yes thank you so much!!!
     
  4. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    and if i use the vehicles asset pack could i use the car scripts on other cars??