Search Unity

How make lock/unlock playable characters in character selection screen?[Solved]

Discussion in 'Scripting' started by Pixitales, May 12, 2019.

  1. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    Using PlayerPref.HasKey is the best idea I got so far then disable the confirm button... How do I make unlockable characters if I press a key or something transitioning from different scenes?

    Here is a character selection script I got from a tutorial. I added my own notes easy for you noobs to understand and I changed a few things.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class CharacterSelection : MonoBehaviour
    6. {
    7.     private int selectionIndex = 0;
    8.  
    9.     private GameObject[] characterList;
    10.  
    11.     //[SerializeField]
    12.     //private Button confirmButton;
    13.  
    14.     void Start()
    15.     {
    16.         //Get last character selected
    17.         selectionIndex = PlayerPrefs.GetInt("CharacterSelected");
    18.  
    19.         if (PlayerPrefs.HasKey("UnlockedCharacterJake"))
    20.         {
    21.             //Unlocked a character named Jake
    22.         }
    23.  
    24.  
    25.         characterList = new GameObject[transform.childCount];
    26.  
    27.         //Get list of available characters
    28.         for (int i = 0; i < transform.childCount; i++)
    29.         {
    30.             characterList[i] = transform.GetChild(i).gameObject;
    31.         }
    32.  
    33.         //Hide unselected characters
    34.         foreach (GameObject go in characterList)
    35.         {
    36.             go.SetActive(false);
    37.         }
    38.  
    39.         //If no saved last character selected, set to default
    40.         characterList[selectionIndex].SetActive(true);
    41.  
    42.     }
    43.  
    44.     public void ToggleLeft()
    45.     {
    46.         //Toggle off current character
    47.         characterList[selectionIndex].SetActive(false);
    48.  
    49.         selectionIndex--;
    50.  
    51.         if (selectionIndex < 0)
    52.         {
    53.             selectionIndex = characterList.Length - 1;
    54.         }
    55.  
    56.         //Toggle on new character
    57.         characterList[selectionIndex].SetActive(true);
    58.  
    59.     }
    60.  
    61.     public void ToggleRight()
    62.     {
    63.         //Toggle off current character
    64.         characterList[selectionIndex].SetActive(false);
    65.  
    66.         selectionIndex++;
    67.  
    68.         if (selectionIndex == characterList.Length)
    69.         {
    70.             selectionIndex = 0;
    71.         }
    72.  
    73.         //Toggle on new character
    74.         characterList[selectionIndex].SetActive(true);
    75.     }
    76.  
    77.     public void ConfirmButton()
    78.     {
    79.         //Save character selected
    80.         PlayerPrefs.SetInt("CharacterSelected", selectionIndex);
    81.  
    82.         //Play game
    83.         SceneManager.LoadScene("Game");
    84.     }
    85. }
    86.  
     
    Last edited: May 12, 2019
  2. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    Nevermind, I think I answered my own question lol. I got it to work by using playpref.has key and disabling button.