transform.Find issue..

Hi Guys, I’ve a problem with my coding, perhaps someone could help with this.

Basically the script below is being called upon a button click, and it’s working.

public void CreateItem (string name)
    {
        if (name == "Bubur" && buburOnScene == null)
        {
            buburOnScene = GenerateBubur();
            buburOnScene.transform.Find(name).gameObject.SetActive(true);
            buburOnScene.GetComponent<Animator>().Play("Wiggle", 0, 0f);
        } else {
            if (buburOnScene != null)
            {
                buburOnScene.transform.Find(name).gameObject.SetActive(true);
                buburOnScene.GetComponent<Animator>().Play("Wiggle", 0, 0f);
            } else {
                print ("Create Bubur First!");
            }
        }
    }

but somehow, under this part of code:

buburOnScene = GenerateBubur();
buburOnScene.transform.Find(name).gameObject.SetActive(true);

It should activate the object, and set the child with the corresponding name value to be activated, but somehow it requires two click execute the setActive line, on first click it looked like the setActive line it is not executed.

GenerateBubur() is a function that returns a GameObject (the activated certain object that have a couple children in it).

Is it clear? Thanks for any help before :slight_smile:

Im not sure if this is the cause and as im using a phone I wont check it.

Wild guess
line 11: shouldnt you have “name” instead of just name? Not sure if strings require it in unity.

Excuse me if im wrong :hushed:

Did you try to debug log the buburOnScene.transform.Find(name) in your code, where the activation takes action?

Well actually name is a string parameter from this line:

public void CreateItem (string name)

And the name string is being send from the button, it is funny though on the second click on that same button it works, it seems like it needed to wait a bit after the GenerateBubur() executed.

did a little test

    public void ButtonClick(InputField inputValue)
    {
        Transform tempT = transform.Find(inputValue.text);
        Debug.Log(tempT);
        tempT.gameObject.SetActive(!tempT.gameObject.activeSelf);
    }

function hook up to a UI button and it’s working fine…

think we’re going to need that long chain instruction broken down into pieces and more debug.log lines to see where it’s going wrong as @WheresMommy suggested :slight_smile:

Yep, I’ve debug it, here is the console screenshot…

2194426--145619--console.jpg

Could it be the problem with my code is the script reside on another empty object, and the variable “buburOnScene” are actually another game object retrieved from the pool using “GenerateBubur()” function, and the script is trying to access the “buburOnScene” child object.

I’ve break the activation code like this:

if (name == "Bubur" && buburOnScene == null)
        {
            buburOnScene = GenerateBubur();
            Transform buburChild = buburOnScene.transform.Find(name);
            Debug.Log(buburChild);
            buburChild.gameObject.SetActive(true);
            buburOnScene.GetComponent<Animator>().Play("Wiggle", 0, 0f);
        }

But the child game object still gets activated after the second click…I’m clueless :frowning:

I’ve tried to debug the active status using activeInHierarchy,

            buburOnScene = GenerateBubur();
            Transform buburChild = buburOnScene.transform.Find(name);
            Debug.Log(buburChild);
            buburChild.gameObject.SetActive(true);
            Debug.Log (buburChild.gameObject.activeInHierarchy);

and it returns True on the second debug.log, but in hierarchy it is disabled…

EDIT: I think the code ignores the first part setActive, and on second click it executes the else part:

else {
            if (buburOnScene != null)
            {
                buburOnScene.transform.Find(name).gameObject.SetActive(true);
                buburOnScene.GetComponent<Animator>().Play("Wiggle", 0, 0f);
            } else {
                print ("Create Bubur First!");
            }
        }

Any ideas to make sure it gets executed on the first if?

Oh wait a second. Could it be, that your gameobject is active inside another inactive gameobject? Could you debug the active in hierarchy thing?

Yup, I thought of that also, but this line:

buburOnScene = GenerateBubur();

Actually activate the parent gameobject first, dunno though if there is a delay on activating, and executing the setActive line, but I got an idea on how to work around this, need to test it first though…

I Finally got it working, by setting the buburOnScene initializing script (another script than this one), too disable all other child gameObject except the one that I need to be activated from start.
Before this, the script disable all the child object, and when pressing the create button, it activates the parent object, but fail to activate the first child object. So as a work around, when it iterates thru its child, the script check for a child with a certain name, if its match, don’t disable the gameObject. And it is working now. Thanks a lot @WheresMommy , @LeftyRighty and @GNGification for the helps :slight_smile:

1 Like