Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

transform.Find issue..

Discussion in 'Scripting' started by romi-fauzi, Jul 8, 2015.

  1. romi-fauzi

    romi-fauzi

    Joined:
    Aug 16, 2013
    Posts:
    161
    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.
    Code (CSharp):
    1. public void CreateItem (string name)
    2.     {
    3.         if (name == "Bubur" && buburOnScene == null)
    4.         {
    5.             buburOnScene = GenerateBubur();
    6.             buburOnScene.transform.Find(name).gameObject.SetActive(true);
    7.             buburOnScene.GetComponent<Animator>().Play("Wiggle", 0, 0f);
    8.         } else {
    9.             if (buburOnScene != null)
    10.             {
    11.                 buburOnScene.transform.Find(name).gameObject.SetActive(true);
    12.                 buburOnScene.GetComponent<Animator>().Play("Wiggle", 0, 0f);
    13.             } else {
    14.                 print ("Create Bubur First!");
    15.             }
    16.         }
    17.     }
    but somehow, under this part of code:
    Code (CSharp):
    1. buburOnScene = GenerateBubur();
    2. 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 :)
     
    Last edited: Jul 8, 2015
  2. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    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 :eek:
     
  3. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Did you try to debug log the buburOnScene.transform.Find(name) in your code, where the activation takes action?
     
  4. romi-fauzi

    romi-fauzi

    Joined:
    Aug 16, 2013
    Posts:
    161
    Well actually name is a string parameter from this line:

    Code (CSharp):
    1. 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.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    did a little test

    Code (csharp):
    1.  
    2.     public void ButtonClick(InputField inputValue)
    3.     {
    4.         Transform tempT = transform.Find(inputValue.text);
    5.         Debug.Log(tempT);
    6.         tempT.gameObject.SetActive(!tempT.gameObject.activeSelf);
    7.     }
    8.  
    9.  
    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 @asd234w4r5 suggested :)
     
  6. romi-fauzi

    romi-fauzi

    Joined:
    Aug 16, 2013
    Posts:
    161
    Yep, I've debug it, here is the console screenshot..
     

    Attached Files:

  7. romi-fauzi

    romi-fauzi

    Joined:
    Aug 16, 2013
    Posts:
    161
    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:

    Code (CSharp):
    1. if (name == "Bubur" && buburOnScene == null)
    2.         {
    3.             buburOnScene = GenerateBubur();
    4.             Transform buburChild = buburOnScene.transform.Find(name);
    5.             Debug.Log(buburChild);
    6.             buburChild.gameObject.SetActive(true);
    7.             buburOnScene.GetComponent<Animator>().Play("Wiggle", 0, 0f);
    8.         }
    But the child game object still gets activated after the second click...I'm clueless :(
     
  8. romi-fauzi

    romi-fauzi

    Joined:
    Aug 16, 2013
    Posts:
    161
    I've tried to debug the active status using activeInHierarchy,

    Code (CSharp):
    1.             buburOnScene = GenerateBubur();
    2.             Transform buburChild = buburOnScene.transform.Find(name);
    3.             Debug.Log(buburChild);
    4.             buburChild.gameObject.SetActive(true);
    5.             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:

    Code (CSharp):
    1. else {
    2.             if (buburOnScene != null)
    3.             {
    4.                 buburOnScene.transform.Find(name).gameObject.SetActive(true);
    5.                 buburOnScene.GetComponent<Animator>().Play("Wiggle", 0, 0f);
    6.             } else {
    7.                 print ("Create Bubur First!");
    8.             }
    9.         }
    Any ideas to make sure it gets executed on the first if?
     
    Last edited: Jul 8, 2015
  9. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Oh wait a second. Could it be, that your gameobject is active inside another inactive gameobject? Could you debug the active in hierarchy thing?
     
  10. romi-fauzi

    romi-fauzi

    Joined:
    Aug 16, 2013
    Posts:
    161
    Yup, I thought of that also, but this line:
    Code (CSharp):
    1. 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..
     
  11. romi-fauzi

    romi-fauzi

    Joined:
    Aug 16, 2013
    Posts:
    161
    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 @asd234w4r5 , @LeftyRighty and @GNGification for the helps :)
     
    GNGification likes this.