Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Der_Kevin · Oct 26, 2015 at 04:48 PM · uigameobjectprefabinterfaceselection

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Der_Kevin · Oct 27, 2015 at 09:09 AM 0
Share

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

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

46 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges