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

I’m trying to access specific children, not all of them.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class CharacterSelect : MonoBehaviour
{

    public GameObject[] characterList;
    private int index;
    private void Start()
    {
        index = PlayerPrefs.GetInt("CharacterSelected");
       
        characterList = new GameObject[transform.childCount];

        // Fill the array with character models
        for (int i = 0; i < transform.childCount; i++)
            characterList[i] = transform.GetChild(i).gameObject;

        // Toggle off character model renderer
        foreach (GameObject go in characterList)
            go.SetActive(false);

        // Toggle on selected character model
        if (characterList[index])
            characterList[0].SetActive(true);
    }

    public void ToggleLeft()
    {
        // Toggle off the current character model
        characterList[index].SetActive(false);
       
        index--;//index -= 1; index = index - 1;
        if (index < 0)
            index = characterList.Length - 1;

        // Toggle on new character model
        characterList[index].SetActive(true);
    }

    public void ToggleRight()
    {
        // Toggle off the current character model
        characterList[index].SetActive(false);

        index++;//index -= 1; index = index - 1;
        if (index == characterList.Length)
            index = 0;

        // Toggle on new character model
        characterList[index].SetActive(true);
    }

    // Load selected charcter model into a scene
    //public void ConfirmButton()
    //{
        //PlayerPrefs.SetInt("CharacterSelected", index);
        //SceneManager.LoadScene("NameOfScene");
    //}

}

Thanks :slight_smile:

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?

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.

// Fill the array with character models
        for (int i = 0; i < transform.childCount; i++)
            characterList[i] = transform.GetChild(i).gameObject;

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…

1 Like

That worked great! Thanks so much!

1 Like