• 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 /
  • Help Room /
avatar image
Question by Ninjix3 · Mar 10, 2018 at 12:10 PM · prefabgameobjectsbreakcase

Switching out GameObject with Prefab?

I want to switch a game object by replacing it with another prefab. I found an easy tutorial to do that but when it goes past case 3 it only switches case 1 and 2. Any possibility it can switch through each as I click. Here is the link to the tutorial. Time starts 1:10. https://www.youtube.com/watch?v=_B1Lc6tUSx8

 public GameObject avatar1, avatar2, avatar3, avatar4, avatar5;

 int whichAvatarIsOn = 1;
 // Use this for initialization
 void Start () {
     avatar1.gameObject.SetActive (true);
     avatar2.gameObject.SetActive (false);
     avatar3.gameObject.SetActive (false);
     avatar3.gameObject.SetActive (false);
     avatar4.gameObject.SetActive (false);
     avatar5.gameObject.SetActive (false);

 }
 
 // Update is called once per frame
 public void SwitchAvatar ()
 {
     switch (whichAvatarIsOn) {
     case 1:
         
         whichAvatarIsOn = 2;

         avatar1.gameObject.SetActive (false);
         avatar2.gameObject.SetActive (true);
         avatar3.gameObject.SetActive (false);
         avatar4.gameObject.SetActive (false);
         avatar5.gameObject.SetActive (false);

         break;

     case 2:
         whichAvatarIsOn = 1;
         avatar1.gameObject.SetActive (true);
         avatar2.gameObject.SetActive (false);
         avatar3.gameObject.SetActive (false);
         avatar4.gameObject.SetActive (false);
         avatar5.gameObject.SetActive (false);

         break;
     case 3:
         whichAvatarIsOn = 4;
         avatar1.gameObject.SetActive (false);
         avatar2.gameObject.SetActive (false);
         avatar3.gameObject.SetActive (true);
         avatar4.gameObject.SetActive (false);
         avatar5.gameObject.SetActive (false);

         break;
     case 4:
         whichAvatarIsOn = 3;
         avatar1.gameObject.SetActive (false);
         avatar2.gameObject.SetActive (false);
         avatar3.gameObject.SetActive (false);
         avatar4.gameObject.SetActive (true);
         avatar5.gameObject.SetActive (false);

         break;
     case 5:
         whichAvatarIsOn = 5;
         avatar1.gameObject.SetActive (false);
         avatar2.gameObject.SetActive (false);
         avatar3.gameObject.SetActive (false);
         avatar4.gameObject.SetActive (false);
         avatar5.gameObject.SetActive (true);

         break;
     }

 }

}

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Hellium · Mar 10, 2018 at 12:22 PM

Here is a complete rework of your solution, but much simpler using an array:

 // Drag & drop the avatars into the array
 public GameObject[] Avatars ;
 private int activeAvatarIndex = -1 ;
 
 // Use this for initialization
 void Start ()
 {
     SwitchAvatar() ;
 }
 
 public void SwitchAvatar ()
 {
     // Disable last avatar
     if( activeAvatarIndex >= 0 )
         Avatars[activeAvatarIndex].SetActive( false );
     
     // Compute next index
     // % is the modulo operator
     activeAvatarIndex = (activeAvatarIndex + 1) % Avatars.Length ;
     
     // Enable new avatar
     Avatars[activeAvatarIndex].SetActive( true );
 }
Comment

People who like this

0 Show 0 · 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

Answer by Bunny83 · Mar 10, 2018 at 12:19 PM

Uhm your code explicitly switches only between case 1 and 2 unless "whichAvatarIsOn" would be set to 3 or 4 in which case your method only switches between case 3 and 4. If you want to iterate through all cases you want to create an actual chain / loop through all of them. When you execute case 1 you set "whichAvatarIsOn" to "2" so the next time you call it case 2 is executed. Of course you would have to set "whichAvatarIsOn" to "3" in case 2. Likewise case 4 should set it to 5 and case 5 probably should set it back to 1.


Though there's a much simpler and more flexible way. Just use an array

 public GameObject[] avatars;
 private int whichAvatarIsOn = 0;
 
 void Start()
 {
     ActivateAvatar(0);
 }
 
 public void NextAvatar()
 {
     ActivateAvatar((whichAvatarIsOn + 1) % avatars.Length);
 }
 
 private void ActivateAvatar(int aIndex)
 {
     whichAvatarIsOn = aIndex;
     for(int i = 0; i < avatars.Length; i++)
         avatars[i].SetActive(i == aIndex);
 }

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

150 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 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 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 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

Advantages of instantiating prefab vs gameObject? 1 Answer

List of unused game objects and prefabs 1 Answer

Making an endless hallway with random objects spawning 0 Answers

Re-opening scene breaks script-prefab connections in assets 0 Answers

Switch Statement Gives Error PLEASE HELP!!! 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