Search Unity

Changing character in game

Discussion in 'Scripting' started by jbowers74321, Jan 17, 2020.

  1. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    I am trying to change my endless runner character in-game. I have a character select menu set up to where I can click the character and it will show up on the character select menu but it won't change the character in the game the pic below shows my character select menu and this is my code for that menu
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Collections.Generic;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.UI;
    7.  
    8. public class CharacterCreation : MonoBehaviour
    9. {
    10.     private List<GameObject> models;
    11.     private int selectionIndex = 0;
    12.     public static int moneyAmount;
    13.     int whichAvaterIsOn = 1;
    14.     public GameObject avatar0, avatar1, avatar2, avatar3, avatar4;
    15.     private readonly string selectedCharacter = "SelectedCharacter";
    16.  
    17.     private void Start()
    18.     {
    19.         models = new List<GameObject>();
    20.         foreach(Transform t in transform)
    21.         {
    22.             models.Add(t.gameObject);
    23.             t.gameObject.SetActive(false);
    24.         }
    25.  
    26.         models[selectionIndex].SetActive(true);
    27.        
    28.     }
    29.  
    30.     public void SwitchAvatar1()
    31.     {
    32.         switch (whichAvaterIsOn)
    33.         {
    34.             case 1:
    35.                 whichAvaterIsOn = 2;
    36.  
    37.                 avatar0.gameObject.SetActive(false);
    38.                 avatar1.gameObject.SetActive(true);
    39.                 avatar2.gameObject.SetActive(false);
    40.                 avatar3.gameObject.SetActive(false);
    41.                 avatar4.gameObject.SetActive(false);
    42.                 break;
    43.         }
    44.     }
    45.  
    46.  
    47.     public void SwitchAvatar2()
    48.     {
    49.         switch (whichAvaterIsOn)
    50.         {
    51.             case 2:
    52.                 whichAvaterIsOn = 1;
    53.  
    54.                 avatar0.gameObject.SetActive(true);
    55.                 avatar1.gameObject.SetActive(false);
    56.                 avatar2.gameObject.SetActive(false);
    57.                 avatar3.gameObject.SetActive(false);
    58.                 avatar4.gameObject.SetActive(false);
    59.                 break;
    60.         }
    61.     }
    62.  
    63.     public void SwitchAvatar3()
    64.     {
    65.         switch (whichAvaterIsOn)
    66.         {
    67.  
    68.             case 3:
    69.                 whichAvaterIsOn = 3;
    70.  
    71.                 avatar0.gameObject.SetActive(false);
    72.                 avatar1.gameObject.SetActive(false);
    73.                 avatar2.gameObject.SetActive(true);
    74.                 avatar3.gameObject.SetActive(false);
    75.                 avatar4.gameObject.SetActive(false);
    76.                 break;
    77.         }
    78.     }
    79.  
    80.     public void SwitchAvatar4()
    81.     {
    82.         switch (whichAvaterIsOn)
    83.         {
    84.  
    85.             case 4:
    86.                 whichAvaterIsOn = 4;
    87.  
    88.                 avatar0.gameObject.SetActive(false);
    89.                 avatar1.gameObject.SetActive(false);
    90.                 avatar2.gameObject.SetActive(false);
    91.                 avatar3.gameObject.SetActive(true);
    92.                 avatar4.gameObject.SetActive(false);
    93.                 break;
    94.         }
    95.     }
    96.  
    97.     public void SwitchAvatar5()
    98.     {
    99.         switch (whichAvaterIsOn)
    100.         {
    101.  
    102.             case 5:
    103.                 whichAvaterIsOn = 5;
    104.  
    105.                 avatar0.gameObject.SetActive(false);
    106.                 avatar1.gameObject.SetActive(false);
    107.                 avatar2.gameObject.SetActive(false);
    108.                 avatar3.gameObject.SetActive(false);
    109.                 avatar4.gameObject.SetActive(true);
    110.                 break;
    111.         }
    112.     }
    113.  
    114.    
    115.  
    116.     private void Update()
    117.     {
    118.        
    119.        
    120.     }
    121.  
    122.     public void Select(int index)
    123.     {
    124.         if (index == selectionIndex)
    125.             return;
    126.  
    127.         if (index < 0 || index >= models.Count)
    128.             return;
    129.  
    130.         models[selectionIndex].SetActive(false);
    131.         selectionIndex = index;
    132.         models[selectionIndex].SetActive(true);
    133.     }
    134.  
    135.    
    136.  
    137. }
    I would just like to know how to change the character in-game when I click on the character button.
     

    Attached Files:

  2. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    does anyone know how to fix this?
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    So, on the simpliest level, yes, I know how to fix it. There are a ton of ways to fix it. But, maybe the easiest fix to understand is to simply create an array of Avatar GameObjects and then have a method tied to all your button clicks that takes a GameObject and when a button is clicked, it loops through and turns them all off. Then it turns on the GameObject that was passed it.

    Code (CSharp):
    1. public GameObject[] avatars; //Drag and drop in the inspector
    2.  
    3.  
    4. //Attach to the onClick method of the button. Drag the avatar GameObject that you want
    5. //the button click to activate into the blank spot for the onClick once the method is hooked in.
    6. public void ActivateAvatar(GameObject targetAvatar)
    7. {
    8.     foreach(GameObject avatar in avatars)
    9.     {
    10.           avatar.SetActive(false);
    11.     }
    12.  
    13.     targetAvatar.SetActive(true);
    14. }
    Now this simply turns on the GameObject you want on. As far as using that model in another scene, you'll need to add additional stuff to track what model needs to be on in the other scene if you are switching scenes.