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 /
This question was closed Jan 24, 2015 at 06:59 PM by Abacab for the following reason:

Other

avatar image
0
Question by Abacab · Jan 23, 2015 at 11:57 PM · c#prefabsetactive

Select Car - Multiple prefabs

Hello!

I have a problem with my car models. I've made it so that the player can choose which car model he uses, and to do that I've added six different car-prefabs to the scene, and then setting all but the selected car model to .SetActive (false).

My problem occurs when I hit play in the Unity editor. All the prefabs that are not active appear briefly at the start in the scene before disappearing. Why is this happening and how can I fix it?

Thanks in advance!

 void Awake ()
         {
                 savedCar = PlayerPrefs.GetInt ("Selected Car");
 
                 car01Prefab = GameObject.Find ("Car01");
                 mainCamera01 = GameObject.Find ("Main Camera 01");
                 car02Prefab = GameObject.Find ("Car02");
                 mainCamera01 = GameObject.Find ("Main Camera 02");
                 car03Prefab = GameObject.Find ("Car03");
                 mainCamera01 = GameObject.Find ("Main Camera 03");
                 car04Prefab = GameObject.Find ("Car04");
                 mainCamera01 = GameObject.Find ("Main Camera 04");
                 car05Prefab = GameObject.Find ("Car05");
                 mainCamera01 = GameObject.Find ("Main Camera 05");
                 car06Prefab = GameObject.Find ("Car06");
                 mainCamera01 = GameObject.Find ("Main Camera 06");
 
                 if (savedCar == 0) {
                         car01Prefab.SetActive (true);
                         mainCamera01.SetActive (true);
                         car02Prefab.SetActive (false);
                         mainCamera02.SetActive (false);
                         car03Prefab.SetActive (false);
                         mainCamera03.SetActive (false);
                         car04Prefab.SetActive (false);
                         mainCamera04.SetActive (false);
                         car05Prefab.SetActive (false);
                         mainCamera05.SetActive (false);
                         car06Prefab.SetActive (false);
                         mainCamera06.SetActive (false);
                 } else if (savedCar == 1) {
                         car01Prefab.SetActive (true);
                         mainCamera01.SetActive (true);
                         car02Prefab.SetActive (false);
                         mainCamera02.SetActive (false);
                         car03Prefab.SetActive (false);
                         mainCamera03.SetActive (false);
                         car04Prefab.SetActive (false);
                         mainCamera04.SetActive (false);
                         car05Prefab.SetActive (false);
                         mainCamera05.SetActive (false);
                         car06Prefab.SetActive (false);
                         mainCamera06.SetActive (false);
                 } else if (savedCar == 2) {
                         car01Prefab.SetActive (false);
                         mainCamera01.SetActive (false);
                         car02Prefab.SetActive (true);
                         mainCamera02.SetActive (true);
                         car03Prefab.SetActive (false);
                         mainCamera03.SetActive (false);
                         car04Prefab.SetActive (false);
                         mainCamera04.SetActive (false);
                         car05Prefab.SetActive (false);
                         mainCamera05.SetActive (false);
                         car06Prefab.SetActive (false);
                         mainCamera06.SetActive (false);
                 } else if (savedCar == 3) {
                         car01Prefab.SetActive (false);
                         mainCamera01.SetActive (false);
                         car02Prefab.SetActive (false);
                         mainCamera02.SetActive (false);
                         car03Prefab.SetActive (true);
                         mainCamera03.SetActive (true);
                         car04Prefab.SetActive (false);
                         mainCamera04.SetActive (false);
                         car05Prefab.SetActive (false);
                         mainCamera05.SetActive (false);
                         car06Prefab.SetActive (false);
                         mainCamera06.SetActive (false);
                 } else if (savedCar == 4) {
                         car01Prefab.SetActive (false);
                         mainCamera01.SetActive (false);
                         car02Prefab.SetActive (false);
                         mainCamera02.SetActive (false);
                         car03Prefab.SetActive (false);
                         mainCamera03.SetActive (false);
                         car04Prefab.SetActive (true);
                         mainCamera04.SetActive (true);
                         car05Prefab.SetActive (false);
                         mainCamera05.SetActive (false);
                         car06Prefab.SetActive (false);
                         mainCamera06.SetActive (false);
                 } else if (savedCar == 5) {
                         car01Prefab.SetActive (false);
                         mainCamera01.SetActive (false);
                         car02Prefab.SetActive (false);
                         mainCamera02.SetActive (false);
                         car03Prefab.SetActive (false);
                         mainCamera03.SetActive (false);
                         car04Prefab.SetActive (false);
                         mainCamera04.SetActive (false);
                         car05Prefab.SetActive (true);
                         mainCamera05.SetActive (true);
                         car06Prefab.SetActive (false);
                         mainCamera06.SetActive (false);
                 } else if (savedCar == 6) {
                         car01Prefab.SetActive (false);
                         mainCamera01.SetActive (false);
                         car02Prefab.SetActive (false);
                         mainCamera02.SetActive (false);
                         car03Prefab.SetActive (false);
                         mainCamera03.SetActive (false);
                         car04Prefab.SetActive (false);
                         mainCamera04.SetActive (false);
                         car05Prefab.SetActive (false);
                         mainCamera05.SetActive (false);
                         car06Prefab.SetActive (true);
                         mainCamera06.SetActive (true);
                 }
 
         }
Comment
Add comment · Show 15
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 Superrodan · Jan 24, 2015 at 12:04 AM 0
Share

I don't know why it's happening... perhaps it's because the find function is slow so while it goes you are still seeing the vehicles?

Regardless, here's a quick fix that wouldn't fix the root of the problem but rather fixes the symptoms. Put all of your cars in an empty gameObject at 0,0,0. Then move that gameObject off screen in your inspector. Once all of the cars are loaded and the one you want is set to Active, just change the transform of that gameObject to 0,0,0.

avatar image Superrodan · Jan 24, 2015 at 12:05 AM 0
Share

I didn't mark that as an answer, however, because someone else will likely come along and offer a real solution. But I know a lot of people right now are working on Global Game Jam games and for those people a quick solution is probably the best one.

avatar image Abacab · Jan 24, 2015 at 12:07 AM 0
Share

Well I am not working on Global Game Jam games, but thank you for the band-aid fix!

avatar image LessThanEpic · Jan 24, 2015 at 12:32 AM 1
Share

As for the original question I would start by making sure the savedCar variable is getting correctly set to a number between 1 and 6. Secondly, I know this wasn't part of your question, but you could cut down on a lot of code by using an array and a loop. I wrote this quick, so it might have some kinks, but here's the general idea:

 private GameObject[] cars;
 private GameObject[] cameras;
 private int numberOfCars = 6;
 
 void Awake()
 {
     savedCar = PlayerPrefs.GetInt ("Selected Car");
     cars = new GameObject[numberOfCars];
     cameras = new GameObject[numberOfCars];
     
     for(int i = 1; i <= numberOfCars; i++)
     {    
         cars[i - 1] = GameObject.Find ("Car0" + i);
         cameras[i - 1] = GameObject.Find ("$$anonymous$$ain Camera 0" + i);
     
         if(savedCar == i)
         {
             cars[i - 1].SetActive(true);
             cameras[i - 1].SetActive(true);
         }
         else
         {
             cars[i - 1].SetActive(false);
             cameras[i - 1].SetActive(false);
         }
     }
     
 }
avatar image pako · Jan 24, 2015 at 09:41 AM 0
Share

@LessThanEpic +1 for improving the original code, but this is not an answer, so I converted it to a comment.

Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by Abacab · Jan 24, 2015 at 06:47 PM

I've got it now. Forgot to add wheel colliders to cars. Just copied each model and decided that I would add wheel colliders later.

No wonder they were "disappearing" from the scene since they just fell through the ground...

Thanks everyone for your help.

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

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Distribute terrain in zones 3 Answers

Activating a prefab in the scene by find it over a tag? 1 Answer

Multiple Cars not working 1 Answer

instantiate within OnTriggerEnter, c# question. 1 Answer

spawning a prefab that have animation 1 Answer


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