- Home /
How to get active Gamobject in array
I made a simple character selection screen with a tutorial I found online (https://www.youtube.com/watch?v=IFTjcPvCZaM), but I'm not doing it the same way he is. I am not using two arrows to cycle through my characters, instead, I made a function to toggle a character on or off with a button. The problem is, I no longer have a cycling index, so I don't know how to get the active characters index. Here is my code, using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CharacterSelect : MonoBehaviour {
public GameObject[] characters;
private int index;
public float rotSpeed;
private Quaternion rot;
void Start() {
rot = transform.rotation;
characters = new GameObject[transform.childCount];
Debug.Log(characters.Length);
for(int i = 0; i < transform.childCount; i++)
{
characters[i] = transform.GetChild(i).gameObject;
}
foreach(GameObject go in characters)
{
go.SetActive(false);
}
if (characters[0])
{
characters[0].SetActive(true);
}
}
private void Update()
{
float hz = Input.GetAxis("Mouse X");
if (Input.GetMouseButton(0))
{
transform.Rotate(0, hz * Time.deltaTime * rotSpeed * -1, 0);
}
}
public void ToggleCharacter(GameObject character)
{
foreach(GameObject go in characters)
{
go.SetActive(false);
}
transform.rotation = rot;
character.SetActive(true);
Debug.Log(characters[index]);
}
}
I just want to know how to get my character's active index so I can spawn him, any help would be appreciated, thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612171514im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
character selection screen help java 1 Answer
Realtime object loading to scene 1 Answer
Selector de personaje, Character selector 0 Answers
Saving character selection 1 Answer