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 /
  • Help Room /
avatar image
0
Question by Impostor Studios · Apr 16, 2016 at 08:47 PM · gameobject

How do I switch characters?,How do I switch caracters? I have this code that works but I cant figure out how to make it work if I have more than 2 characters. Please help.

using UnityEngine; using System.Collections;

 public class CubeSwitcher : MonoBehaviour {
 
     GameObject LCube, TCube, UCube, ZCube,ZCube2;
     int playerSelect;
 
     // Use this for initialization
     void Start () {
         playerSelect = 1;
         LCube = GameObject.Find("LCube");
         TCube = GameObject.Find("TCube");
 
         TCube.SetActive (false);
 
     }
 
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown (KeyCode.DownArrow))
         {
             if (playerSelect == 1) {
                 playerSelect = 2;
             } else if (playerSelect == 2) 
             {
                 playerSelect = 1;
             }
         }
         if (playerSelect == 1) {
             LCube.SetActive (true);
             TCube.SetActive (false);
         } else if (playerSelect == 2) 
         {
             LCube.SetActive (false);
             TCube.SetActive (true);
         }
     }
 }
 


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

1 Reply

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

Answer by Scribe · Apr 17, 2016 at 11:24 AM

You ought to look up some tutorials around using arrays and/or lists as when you have a large or unknown number of things to save that are related to each other, is prime use! Here's an example of how you might solve your problem:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CubeSwitcher : MonoBehaviour {
     string[] cubePrefix; //array of prefixes to append to "cube" to lookup gameobject names
     GameObject[] cubes; //the cubes that we find
     int playerCount; //the number of cubes found
     int playerSelect; //the current active cube
 
     void Start () {
         cubePrefix = new string[3]{"L", "T", "Z"};
         List<GameObject> c = new List<GameObject>();
         
         playerSelect = 0;
 
         for(int i = 0; i < playerCount; i++){
             GameObject go = GameObject.Find(cubePrefix[i] + "Cube");
             if(go != null){ //if we didn't find the object, skip it!
                 if(c.Count == playerSelect){
                     go.SetActive(true); //if this cube is the currently selected one, activate!
                 }else{
                     go.SetActive(false); //else deactivate
                 }
                 c.Add(go); //then add it to our list and carry on
             }
         }
 
         cubes = c.ToArray(); //make our list an array
         playerCount = cubes.Length; //save the number of objects found
     }
 
     // Update is called once per frame
     void Update(){
         if (Input.GetKeyDown(KeyCode.DownArrow)){
             playerSelect = (playerSelect+1)%playerCount;
             // the '%' in 'a%b' returns the remainder after the division of a/b
             //hence this moves through our objects cyclically
             //(the last item goes back to the first item)
             //look up modulo arithmetic for more info!
 
             for(int i = 0; i < playerCount; i++){
                 cubes[i].SetActive(i == playerSelect); //set active if currently selected
             }
         }
     }
 }

hope that helps,

Scribe

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

63 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

Related Questions

Move GameObject in a direction of a mouse but longer 1 Answer

Assign an image as a texture to a mesh 1 Answer

How to Play sequentially more than one AudioSource in one game object ? 1 Answer

How can i create a GameObject > UI > Raw Image in alternative way. because there's nothing appears in my unity. Please help. thanks. 0 Answers

Need help with GameObject placement 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