How to enable a children of a clone object in Unity?

829 Views Asked by At

I hold my clones in a list for GUI operations in Unity3D & C#.

button = Instantiate(BlackBox, Vector2.zero, Quaternion.identity) as GameObject;
buttons.Add(button);

How to enable ArrowRight in this situation?

I want to reach ArrowRight to enable it. I tried variations of this kind of methods:

buttons[index].gameObject.transform.GetChild(1).gameObject.SetActive(true);

but it doesn't work. Objects' components.

2

There are 2 best solutions below

2
On BEST ANSWER

Solved:

instead of:

buttons[index].gameObject.transform.GetChild(1).gameObject.SetActive(true);

use:

buttons[index].transform.GetChild(1).gameObject.SetActive(true);
3
On

I just quickly got this code to fix your issue. Maybe not the best solution but it should work if I understood your question correctly.

using UnityEngine;

public class blackBox : MonoBehaviour {
    public bool toggleRight;

    // Update is called once per frame
    void Update () {
        if(toggleRight)
        {
            foreach(Transform child in transform)
            {
                if(child.name.Equals("ArrowRight"))
                {
                    child.gameObject.SetActive(!child.gameObject.activeSelf);
                }
            }
            toggleRight = false;
        }
    }
}

This is just a demo, rip it up to suite your needs.

Place that on the blackbox gameobject and well use the foreach to activate or deactivate the gameobject with said name.