Search Unity

Car selection screen

Discussion in 'Scripting' started by hellobard, Dec 6, 2012.

  1. hellobard

    hellobard

    Joined:
    Sep 26, 2012
    Posts:
    140
    Hi,

    I am making a car game and I would like to have a Car selection screen in my first scene, and then load an instantiated prefab from the selection in the next scene. So how I understand this process is that essentially I need the player to press a button for selection his/her car which then sends a message to the next scene to instantiate a certain prefab.

    Does anyone know how to set up a script like this? Any help would be highly appreciated! :)
     
  2. achingupta87

    achingupta87

    Joined:
    Apr 9, 2012
    Posts:
    144
    you needs to take an for loop for all car button. suppose you have 5 car . put all car info in an arrayList.
    Code (csharp):
    1.  
    2. public string[] cararray=new string[5];
    3.  
    4.      void Start ()
    5.             {
    6.         cararray[0]=car1;
    7.         cararray[1]=car2;
    8.         cararray[2]=car3;
    9.         cararray[3]=car4;
    10.         cararray[4]=car5;
    11.        
    12.         }      
    13.  
    14. foreach(int i=0;i<=5;i++)
    15. {
    16.            if(GUILayout.Button(""+cararray[i],GUILayout.ExpandWidth(true)))
    17.             {
    18.                           carname=cararray[i];
    19.                         // use any function to call this car here
    20.  
    21.                         }
    22. }
    23.  
     
    Last edited: Dec 6, 2012
  3. Ravel

    Ravel

    Joined:
    Nov 21, 2010
    Posts:
    605
    You will also need to transfer your car from selection scene to game scene, use DontDestroyOnLoad(YourSelectedCar);
     
  4. hellobard

    hellobard

    Joined:
    Sep 26, 2012
    Posts:
    140
    Many thanks to both of you for the replies and the excellent code from achingupta87!

    Ravel: so I will need to bring it with me to the next scene? Interesting... So I could have a show room of 5 cars, if I choose one, then I set a DontDestroyOnLoad on the car chosen and Destroy the others right?

    Or I guess it could be solved with adding code to the button that Instantiates the Prefab in the menu scene and adding a DontDestroyOnLoad to that Prefab.
     
  5. SuryaPrakashModi

    SuryaPrakashModi

    Joined:
    Mar 11, 2018
    Posts:
    9