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 ![]()