Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Alex_TNT · Mar 24, 2015 at 06:20 AM · unity 5javascript

Change car models on keypress

Can someone help me minimize and develop more of this code, There are 24 different cars 12 normal 12 cop cars, would be nice if I could press a key ( Spacebar) and would cycle/change trough normal cars on every press, and once I press a specific key let's say (P) from police it will switch to that version of cop car

 var model_01: GameObject;
 var model_02: GameObject;
 var model_03: GameObject;
 var model_04: GameObject;
 var model_05: GameObject;
 var model_06: GameObject;
 var model_07: GameObject;
 var model_08: GameObject;
 var model_09: GameObject;
 var model_10: GameObject;
 var model_11: GameObject;
 var model_12: GameObject;
 
 
 function Start()
 {
 
 model_01.SetActive(true);
 model_02.SetActive(false);
 model_03.SetActive(false);
 model_04.SetActive(false);
 model_05.SetActive(false);
 model_06.SetActive(false);
 model_07.SetActive(false);
 model_08.SetActive(false);
 model_09.SetActive(false);
 model_10.SetActive(false);
 model_11.SetActive(false);
 model_12.SetActive(false);
 }
 
 function Update(){
 if (Input.GetKeyDown ("1"))
 {
 model_01.SetActive(true);
 model_02.SetActive(false);
 model_03.SetActive(false);
 model_04.SetActive(false);
 model_05.SetActive(false);
 model_06.SetActive(false);
 model_07.SetActive(false);
 model_08.SetActive(false);
 model_09.SetActive(false);
 model_10.SetActive(false);
 model_11.SetActive(false);
 model_12.SetActive(false);
 }
 else if  (Input.GetKeyDown ("2"))
 {
 model_01.SetActive(false);
 model_02.SetActive(true);
 model_03.SetActive(false);
 model_04.SetActive(false);
 model_05.SetActive(false);
 model_06.SetActive(false);
 model_07.SetActive(false);
 model_08.SetActive(false);
 model_09.SetActive(false);
 model_10.SetActive(false);
 model_11.SetActive(false);
 model_12.SetActive(false);
 }
 else if  (Input.GetKeyDown ("3"))
 {
 model_01.SetActive(false);
 model_02.SetActive(false);
 model_03.SetActive(true);
 model_04.SetActive(false);
 model_05.SetActive(false);
 model_06.SetActive(false);
 model_07.SetActive(false);
 model_08.SetActive(false);
 model_09.SetActive(false);
 model_10.SetActive(false);
 model_11.SetActive(false);
 model_12.SetActive(false);
 }
 else if  (Input.GetKeyDown ("4"))
 {
 model_01.SetActive(false);
 model_02.SetActive(false);
 model_03.SetActive(false);
 model_04.SetActive(true);
 model_05.SetActive(false);
 model_06.SetActive(false);
 model_07.SetActive(false);
 model_08.SetActive(false);
 model_09.SetActive(false);
 model_10.SetActive(false);
 model_11.SetActive(false);
 model_12.SetActive(false);
 }
 else if  (Input.GetKeyDown ("5"))
 {
 model_01.SetActive(false);
 model_02.SetActive(false);
 model_03.SetActive(false);
 model_04.SetActive(false);
 model_05.SetActive(true);
 model_06.SetActive(false);
 model_07.SetActive(false);
 model_08.SetActive(false);
 model_09.SetActive(false);
 model_10.SetActive(false);
 model_11.SetActive(false);
 model_12.SetActive(false);
 }
 else if  (Input.GetKeyDown ("6"))
 {
 model_01.SetActive(false);
 model_02.SetActive(false);
 model_03.SetActive(false);
 model_04.SetActive(false);
 model_05.SetActive(false);
 model_06.SetActive(true);
 model_07.SetActive(false);
 model_08.SetActive(false);
 model_09.SetActive(false);
 model_10.SetActive(false);
 model_11.SetActive(false);
 model_12.SetActive(false);
 }
 else if  (Input.GetKeyDown ("7"))
 {
 model_01.SetActive(false);
 model_02.SetActive(false);
 model_03.SetActive(false);
 model_04.SetActive(false);
 model_05.SetActive(false);
 model_06.SetActive(false);
 model_07.SetActive(true);
 model_08.SetActive(false);
 model_09.SetActive(false);
 model_10.SetActive(false);
 model_11.SetActive(false);
 model_12.SetActive(false);
 }
 else if  (Input.GetKeyDown ("8"))
 {
 model_01.SetActive(false);
 model_02.SetActive(false);
 model_03.SetActive(false);
 model_04.SetActive(false);
 model_05.SetActive(false);
 model_06.SetActive(false);
 model_07.SetActive(false);
 model_08.SetActive(true);
 model_09.SetActive(false);
 model_10.SetActive(false);
 model_11.SetActive(false);
 model_12.SetActive(false);
 }
 else if  (Input.GetKeyDown ("9"))
 {
 model_01.SetActive(false);
 model_02.SetActive(false);
 model_03.SetActive(false);
 model_04.SetActive(false);
 model_05.SetActive(false);
 model_06.SetActive(false);
 model_07.SetActive(false);
 model_08.SetActive(false);
 model_09.SetActive(true);
 model_10.SetActive(false);
 model_11.SetActive(false);
 model_12.SetActive(false);
 }
 else if  (Input.GetKeyDown ("0"))
 {
 model_01.SetActive(false);
 model_02.SetActive(false);
 model_03.SetActive(false);
 model_04.SetActive(false);
 model_05.SetActive(false);
 model_06.SetActive(false);
 model_07.SetActive(false);
 model_08.SetActive(false);
 model_09.SetActive(false);
 model_10.SetActive(true);
 model_11.SetActive(false);
 model_12.SetActive(false);
 }
 else if  (Input.GetKeyDown ("-"))
 {
 model_01.SetActive(false);
 model_02.SetActive(false);
 model_03.SetActive(false);
 model_04.SetActive(false);
 model_05.SetActive(false);
 model_06.SetActive(false);
 model_07.SetActive(false);
 model_08.SetActive(false);
 model_09.SetActive(false);
 model_10.SetActive(false);
 model_11.SetActive(true);
 model_12.SetActive(false);
 }
 else if  (Input.GetKeyDown ("="))
 {
 model_01.SetActive(false);
 model_02.SetActive(false);
 model_03.SetActive(false);
 model_04.SetActive(false);
 model_05.SetActive(false);
 model_06.SetActive(false);
 model_07.SetActive(false);
 model_08.SetActive(false);
 model_09.SetActive(false);
 model_10.SetActive(false);
 model_11.SetActive(false);
 model_12.SetActive(true);
 }
 }
 
 

Comment
Add comment · Show 6
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 abi-kr01 · Mar 24, 2015 at 06:31 AM 0
Share

Run this thing in update() ins$$anonymous$$d of Start()

avatar image Alex_TNT · Mar 24, 2015 at 06:56 AM 0
Share

it is running in Update(), it just declaring what to be visible at Start() and what not

avatar image soft_sound · Mar 25, 2015 at 12:59 AM 0
Share

Why not loop through the cars and check if it has a certain name then display said car, so name police cars police$$anonymous$$odelA, police$$anonymous$$odelB, etc and check if the name contains "police" or "cop". etc

Also I personally would just use 4 controls, left and right to go down the list, and up and down to swap car type. I would put the cars into different lists or layer the list so 1-4 is a cop car and 5-8 is some other car and when you are searching for cop cars if you get to element 5 you either stop at 4 or jump back to 1.

etc...

avatar image smallbit · Mar 27, 2015 at 09:31 AM 0
Share

Use Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha0) for "0" Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha1) for "1" etc...

also there are tons of ways to make your code clearer. For instance you could put them into array of type Transform[] than write a method that resets all cars (iterate over array). and than on each button you could just first reset all the cars and than call array[0].gameObject.SetActive(true). for example...

avatar image Parzivell · Mar 27, 2015 at 11:00 AM 0
Share

Ins$$anonymous$$d of using model_11.SetActive(false); Use model_11.gameObject.SetActive(false); I'm sure that's your problem

Show more comments

1 Reply

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

Answer by Alex_TNT · Mar 27, 2015 at 11:18 AM

code on unity forum I made with help of 2 guys

 public var cars : GameObject[];
 public var carsPolice : GameObject[];
 // What car is active at start
 public var carNumber:int = 2;
  
  
 function Start ()
 {
    for( var i: int=0; i < cars.Length; i++){
    cars[i].SetActive(false);
    }
    cars[carNumber].SetActive(true);
 }
  
   function Update(){
      if (Input.GetKeyUp ("space"))
   {
   if(carNumber < cars.Length -1) {
   carNumber++;
   } else {
   carNumber=0;
   }
   changeCars(carNumber);
   }
      // Toggle Police Version of the car
      if (cars[carNumber].activeInHierarchy == true && Input.GetKeyDown ("p")){
      cars[carNumber].SetActive(false);
      carsPolice[carNumber].SetActive(true);
      }
      else if(Input.GetKeyDown ("p"))
      {
      cars[carNumber].SetActive(true);
      carsPolice[carNumber].SetActive(false);
      }
      //
   }
    
   function changeCars(carNumber:int){
   for( var i: int=0; i < cars.Length; i++){
   cars[i].SetActive(false);
        carsPolice[i].SetActive(false);
   }
   cars[carNumber].SetActive(true);
  
   }
 


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

6 People are following this question.

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

Related Questions

How, from scratch, can I detect the angle of the ground underfoot a character not controlled by a character controller. 1 Answer

BCE0019 "enabled" is not a member of "Object"(On JavaScript Script) 2 Answers

How do I know the reason of not running my game smooth? 0 Answers

Gun shooting help 2 Answers

Only error comes when try to build for android not for windows? 0 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