Search Unity

Character Select - edit code from child index to (custom) child index?

Discussion in 'Scripting' started by Proto-G, May 6, 2021.

  1. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    I'm trying to access specific children, not all of them.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class CharacterSelect : MonoBehaviour
    7. {
    8.  
    9.     public GameObject[] characterList;
    10.     private int index;
    11.     private void Start()
    12.     {
    13.         index = PlayerPrefs.GetInt("CharacterSelected");
    14.        
    15.         characterList = new GameObject[transform.childCount];
    16.  
    17.         // Fill the array with character models
    18.         for (int i = 0; i < transform.childCount; i++)
    19.             characterList[i] = transform.GetChild(i).gameObject;
    20.  
    21.         // Toggle off character model renderer
    22.         foreach (GameObject go in characterList)
    23.             go.SetActive(false);
    24.  
    25.         // Toggle on selected character model
    26.         if (characterList[index])
    27.             characterList[0].SetActive(true);
    28.     }
    29.  
    30.     public void ToggleLeft()
    31.     {
    32.         // Toggle off the current character model
    33.         characterList[index].SetActive(false);
    34.        
    35.         index--;//index -= 1; index = index - 1;
    36.         if (index < 0)
    37.             index = characterList.Length - 1;
    38.  
    39.         // Toggle on new character model
    40.         characterList[index].SetActive(true);
    41.     }
    42.  
    43.     public void ToggleRight()
    44.     {
    45.         // Toggle off the current character model
    46.         characterList[index].SetActive(false);
    47.  
    48.         index++;//index -= 1; index = index - 1;
    49.         if (index == characterList.Length)
    50.             index = 0;
    51.  
    52.         // Toggle on new character model
    53.         characterList[index].SetActive(true);
    54.     }
    55.  
    56.     // Load selected charcter model into a scene
    57.     //public void ConfirmButton()
    58.     //{
    59.         //PlayerPrefs.SetInt("CharacterSelected", index);
    60.         //SceneManager.LoadScene("NameOfScene");
    61.     //}
    62.  
    63. }
    Thanks :)
     
  2. And what is your exact question? Because you're just doing that as far I can judge the code you put here. You're accessing individual children to toggle them on and off.
    Is this code not working in some way? If it's the case, how is it not working and what is it you're expecting to happen?
     
  3. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    This section of code is gathering all of the child models. I would to drag specific child models into the public list, not all of them.
    Code (CSharp):
    1. // Fill the array with character models
    2.         for (int i = 0; i < transform.childCount; i++)
    3.             characterList[i] = transform.GetChild(i).gameObject;
     
  4. Comment out the 15th, 18th and 19th lines in your script and drag and drop your game objects you want to see in the inspector.

    Also replace the 13th line with this, so it will work when you don't have anything in your playerprefs.
    index = PlayerPrefs.GetInt("CharacterSelected", 0);


    And I think it should work. Currently I don't have the chance to try it out for you because we have a multi-hour power outage...
     
    Proto-G likes this.
  5. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    That worked great! Thanks so much!
     
    Lurking-Ninja likes this.