- Home /
 
Change GameObject According to UI selection
Hey! I am currently working on a racing game and sitting at at the car selection screen. here is my current code:
 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class SlotSelector : MonoBehaviour {
     public RectTransform navigator1;
     int nav1Pos = 0;
     public RectTransform navigator2;
     int nav2Pos = 0;
     
     public RectTransform[] slots = new RectTransform[12];
     public int jumpAmount = 4;
     public Text textShowNav1;
     public Text textShowNav2;
     void Start(){
         MoveNav1(0);
         MoveNav2(0);
     }
     void Update () {
         // move up
         if(Input.GetKeyDown(KeyCode.W)){
             MoveNav1(-jumpAmount);
         }
         if(Input.GetKeyDown(KeyCode.UpArrow)){
             MoveNav2(-jumpAmount);
         }
         
         if(Input.GetKeyDown(KeyCode.A)){
             MoveNav1(-1);
         }
         if(Input.GetKeyDown(KeyCode.LeftArrow)){
             MoveNav2(-1);
         }
         
         if(Input.GetKeyDown(KeyCode.S)){
             MoveNav1(jumpAmount);
         }
         if(Input.GetKeyDown(KeyCode.DownArrow)){
             MoveNav2(jumpAmount);
         }
         
         if(Input.GetKeyDown(KeyCode.D)){
             MoveNav1(1);
         }
         if(Input.GetKeyDown(KeyCode.RightArrow)){
             MoveNav2(1);
         }
     }
     
     void MoveNav1(int change){
         if(change > 0){
             if(nav1Pos+change < slots.Length-1){
                 nav1Pos += change;
             }else{
                 nav1Pos = slots.Length-1;
             }
         }else{
             if(nav1Pos+change >= 0){
                 nav1Pos += change;
             }else{
                 nav1Pos = 0;
             }
         }
         navigator1.position = slots[nav1Pos].position;
         textShowNav1.text = "Nav 1 is at slot "+ nav1Pos;
     }
     
     void MoveNav2(int change){
         if(change > 0){
             if(nav2Pos+change < slots.Length-1){
                 nav2Pos += change;
             }else{
                 nav2Pos = slots.Length-1;
             }
         }else{
             if(nav2Pos+change >= 0){
                 nav2Pos += change;
             }else{
                 nav2Pos = 0;
             }
         }
         navigator2.position = slots[nav2Pos].position;
         textShowNav2.text = "Nav 2 is at slot "+ nav2Pos;
     }
 }
 
               which looks in game like this: .. cant upload gif, so here a direct link: http://www.gfycat.com/FlimsyHonorableGentoopenguin
what i want to do now is, that, every time i switch to another slot and press the Enter Key, the Car on the left (the pizza wagon) changes to another Car prefab. do you guys have an idea how i can do this? Thanks!
Answer by gkillz · Oct 26, 2015 at 07:24 PM
You will need to add the following code to your current class to do it,
 void Update () {
      //... all ur existing code
     if( /*<enter is pressed condition>*/ ){
          selectNav1(nav1Pos);
     }
 }
 public Transform[] cars; // all 12 cars meant to be shown, can be set from inspector
 public Transform displayAt; // the position where the car is supposed to be displayed
 public Transform selectedCar;// the car which is currently being displayed
 
 void selectNav1(int nav1Pos){
       selectedCar.gameObject.SetActive(false); //deactivate the previously selected car
       selectedCar = cars[nav1Pos]; // assign the selected car
       selectedCar.position = displayAt.position; //move it to correct position
       selectedCar.gameObject.SetActive(true); // set the car active
 }
 
 
               will come back later to add more details
hey! thanks for the detailed answer! somehow this:
 public Transform cars[]; // all 12 cars meant to be shown,
 
                  is causing troubles
syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type
should be someting like this right?
 public Transform[] cars = new Transform[12];
 
                  but then it marks me the
 selectedCar.setActive(false);
 
                  as wrong
Answer by Socapex · Oct 26, 2015 at 06:37 PM
In your SlotSelector script, add a reference to a placeholder prefab that will display the car. ex. public GameObject blee; Drag CarDisplay prefab onto the variable in editor. 
When you move, call a script that has either an id or a model to display: blee.GetComponent<CarDisplay>().changeCar(int id); or something like that. Cheers 
[edit] It seems you are already doing exactly that and I misunderstood what you where looking for. Use an empty prefab, that spawns or destroys objects when you change cars. Here is a pseudo code for you, I did not test this but it should give you a very clear idea of what I mean:
 // Also holds cars in an array of GameObjects
 GameObject selectedCar;
 
 void changeCar(int id) {
 // Remove previously selected car
 Destroy(selectedCar);
 
 // Create object
 selectedCar = Instantiate(myCars[id], new Vector3(0,0,0), Quaternion.identity) as GameObject;
 }
 
               More info: http://docs.unity3d.com/ScriptReference/Object.Instantiate.html and: http://docs.unity3d.com/ScriptReference/Object.Destroy.html
IIRC, the gameObject may be destroyed at the next frame. So you might want to either use LateUpdate for spawning with some toggle, or use a coroutine to similar effect.
Your answer
 
             Follow this Question
Related Questions
UI Selection in GameObject dropdown out of place? 1 Answer
Prefab not showing preview 0 Answers
SetActive modifies Prefab 1 Answer
Disable GameObject through Prefab? 2 Answers
Reset an GO to its original state (start app state) 2 Answers