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() weirdly always return null

Discussion in 'Scripting' started by ardiawanbagusharisa, Dec 28, 2017.

  1. ardiawanbagusharisa

    ardiawanbagusharisa

    Joined:
    Oct 26, 2015
    Posts:
    9
    Hello guys.
    I have a hierarchy of GameObject in unity like this:
    Code (CSharp):
    1.     /*Hierarchy
    2.     Tile   <-- as parent
    3.      Enemy <-- as child
    4.     note: "Enemy" is an instance of a prefab.
    5.     */

    by using this 1st method, I can keep the "Enemy" in a variable ("go"):


    Code (CSharp):
    1.  
    2.     // 1st method, working
    3.     foreach(transform t in Tile.transform){
    4.         if(t.name == ("Enemy")) {
    5.             GameObject go = t.gameObject;
    6.             <do something with go>
    7.         }
    8.     }
    but if I do as 2nd method:


    Code (CSharp):
    1.  
    2.     // 2nd method, not working
    3.     GameObject go = Tile.transform.Find("Enemy").gameObject; // always return null;
    as I state on the comment, the second method always returning null. I really don't understand why. I use the 2nd method on the different part of scripts and it works normally.
    I did browse similar threads in this forum. I'm sure I'm not making mistake on the gameobject's name.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Is Tile parented to anything?
     
  3. ardiawanbagusharisa

    ardiawanbagusharisa

    Joined:
    Oct 26, 2015
    Posts:
    9
    Hey. Yes it does.
    upload_2017-12-29_21-57-23.png
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    He should only have to use the slash if he was targeting Tiles for example. Otherwise, if he's targeting Tile (8) and looking for it's children, transform.Find should work.

    Personally never had issues with Transform.Find, so I would suspect you have something else going on. Perhaps a timing issue.

    I would add some debugs before your second method. Print out Tile.transform.ChildCount for example to see if it even has children at that time. If you have to go deeper, you may need to step through your code.
     
  6. ardiawanbagusharisa

    ardiawanbagusharisa

    Joined:
    Oct 26, 2015
    Posts:
    9
    Thanks for you both. But that's not the case. Because I used the 2nd method on different time and it works as well. I did check the number of children and it always returns as expected. I will trace my code again.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    I understand it worked elsewhere, but that doesn't mean it's correct here if it isn't working. :)

    Is your Enemy active when you try to Find it?
     
  8. inyourpc

    inyourpc

    Joined:
    Jul 14, 2013
    Posts:
    9
    Transform.Find did not work for me,direct child reference works for me through serializefield, for some reason. I willl consider it stable. I am using latest 2021.3.11f1 LTs version.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    inyourpc and Niter88 like this.
  10. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,366
    Most likely you have the same script on more than one object and one of the other objects (the one you're not looking at) has no children and is causing the error.