Search Unity

[SOLVED] GameObject.Find doesn't work

Discussion in 'iOS and tvOS' started by bbvrdev, Sep 6, 2009.

  1. bbvrdev

    bbvrdev

    Joined:
    Aug 11, 2009
    Posts:
    221
    Hey guys,

    I'm hoping you can help me with this problem. I've been writing the code for our game for the last 2 months, and in that time, I've completely avoided using GameObject.Find, because I've never been able to make it work. I have tried every syntax derivation I can think of, and still, it always returns Null.

    The funny thing is, we copied a project from my coworker's computer, and the .Find code that was working there does NOT work on mine, even though the actual objects in the hierarchy, and the code was the same!

    Can anyone help shed some light on this? Am I doing something catastrophically wrong?

    Here's the code I'm wrestling with. CoreRoot2 is the name of a game object in the hierarchy. gameLogic is the name of a script attached to that object. I'm using Unity 1.5, but had this same problem on the previous version.

    Code (csharp):
    1.  
    2. var temp = GameObject.Find("CoreRoot2");
    3. var script = temp.GetComponent(gameLogic) as gameLogic;
    4.  
    All I intend to do is access a public function from the gameLogic script, that's it. Nothing crazy.

    I appreciate any help you can give me! :)
     
  2. bbvrdev

    bbvrdev

    Joined:
    Aug 11, 2009
    Posts:
    221
    Wow, I knew all I'd have to do is post on the forum and it would magically start working. Isn't that always the way?

    If anyone else is stuck on this n00b problem:
    Code (csharp):
    1.  
    2. var temp : GameObject;
    3. temp = GameObject.Find("CoreRoot2");
    4.  
    Gets the object just fine. Unfortunately, I still can't get the script, I get "not a member of UnityEngine.Component."

    Anyone?
     
  3. jtbentley

    jtbentley

    Joined:
    Jun 30, 2009
    Posts:
    1,397
    Just a question, is the game object deactivated? The Find will not find objects that are not enabled.

    Just looking at it though, it looks kinda fine :/

    I also make a habit of explicitly specifying where in the hierachy it is, for example

    Code (csharp):
    1.  
    2. var awesomeObject : GameObject;
    3. awesomeObject = GameObject.Find("/AwesomeStuff/AwesomeObject");
    4.  
    .. This might not solve anything, but might be food for thought.
     
    Supahtrupah, Blitz54, llu18 and 5 others like this.
  4. jtbentley

    jtbentley

    Joined:
    Jun 30, 2009
    Posts:
    1,397
    By the way, 12 thumbs up for a dune reference.
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    I would start to recommend that you declare your variables.

    Automagic does not work with JS on the iphone.


    Code (csharp):
    1. var temp : GameObject = GameObject.Find("CoreRoot2");
    2. var someScript : gameLogic = temp.GetComponent(gameLogic) as gameLogic;
    3. someScript.ThisIsSomeMagicPublicFunction();
     
  6. bbvrdev

    bbvrdev

    Joined:
    Aug 11, 2009
    Posts:
    221
    Thanks a lot, JTBentley,

    Actually, I've got it finding the gameobject now, but I can't seem to get the script. This is what I'm messing with at the moment:

    Code (csharp):
    1.  
    2. var other : gameLogic = temp.GetComponent(gameLogic);
    3.  
    It seems like it should work, but I get "object reference not set to an instance of an object"... The game object and the script are both enabled btw :)

    Fellow Dune fan, eh? Awesome... I've read it so many times, what a great book! You ever seen the 4 hour director's cut of the old movie?
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    both steps are required. Correct variable type + casting.

    No cast = no gameLogic object :)
    No correct variable type = potentially no gameLogic object

    Cast + Correct Variable type = either null or correct gameLogic object
     
  8. bbvrdev

    bbvrdev

    Joined:
    Aug 11, 2009
    Posts:
    221
    Thank you dreamora, I appreciate your help!

    I tried your code, but it still returns null for the script... any clues?
     
  9. bbvrdev

    bbvrdev

    Joined:
    Aug 11, 2009
    Posts:
    221
    dreamora:
    I've got it catching the gameobject, but strangely, if I put it into one line:

    Code (csharp):
    1.  
    2. var temp : GameObject = GameObject.Find("CoreRoot2");
    3.  
    it says I'm not allowed to call this function when declaring a variable. It's working the other way, but I don't understand why this is...
     
  10. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    if it returns null then the GameObject stored in temp has no component gameLogic

    Perhaps its GameLogic, gamelogic or something else. (not 100% sure if JS on the iphone is case sensitive there)
     
  11. jtbentley

    jtbentley

    Joined:
    Jun 30, 2009
    Posts:
    1,397
    The object does not exist? :)

    Btw, just reiterating the hierachy there.. Without a / at the front (searching from root), the object will only search underneath the object's parent.

    Blah
    - Blah Child 1
    - Blah Child 2
    - Blah Child 3

    If Blah Child searches, it will only search in everything underneath Blah, unless you specify otherwise.
     
  12. bbvrdev

    bbvrdev

    Joined:
    Aug 11, 2009
    Posts:
    221
    gameLogic definitely exists, other scripts are calling static variables from it... Regarding the hierarchy, do I need to call the parent to find the script?

    My hierarchy is:

    CoreRoot2 (with attached gameLogic script)
    -- Child 1 (with the current script I'm calling from)

    Could it not be working because the script I need is on the parent? I already have CoreRoot2 being called in a variable.
     
  13. jtbentley

    jtbentley

    Joined:
    Jun 30, 2009
    Posts:
    1,397
    Oh, well if the gameobject is being found, err... gameObject.Find("somescript").Component maybe?

    Drawing a blank :)
     
  14. bbvrdev

    bbvrdev

    Joined:
    Aug 11, 2009
    Posts:
    221
    I just tried swapping the two around so that Child1 is the parent of CoreRoot2, but that didn't make any difference.

    I appreciate your help though guys, thanks for such quick replies.
     
  15. bbvrdev

    bbvrdev

    Joined:
    Aug 11, 2009
    Posts:
    221
    For clarity, this is exactly what I'm working with here:

    Code (csharp):
    1.  
    2. var temp : GameObject;
    3. temp = GameObject.Find("CoreRoot2");
    4.  
    5. var someScript : gameLogic = temp.GetComponent(gameLogic) as gameLogic;
    6.  
    7. if (temp == null) {
    8.     print("No gameobject found with that name!");
    9.   } else {
    10.     print("gameobject: " + temp.name);
    11.     if (someScript == null) {
    12.         print("No object found with that name!");
    13.     } else {
    14.         print("someScript name: " + someScript.name);
    15.         //someScript.test();
    16.     }
    17. }
    18.  
    temp returns CoreRoot2, someScript returns null.
     
  16. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    You need to ask the object that has the component for the component.
    if its the parent, don't use find at all, use transform.parent.gameObject instead.


    Also, is the case right?
    Sure the class isn't called GameLogic? (thats normal naming convention, gameLogic would be a variable name)
     
  17. bbvrdev

    bbvrdev

    Joined:
    Aug 11, 2009
    Posts:
    221
    Thanks dreamora,

    The script is called gameLogic, is this not a good naming convention for a javascript name?

    I'm sorry for my n00bness, but would you please help me with the code for using transform.parent.gameObject? I don't quite get it. Can I call it as a variable the same way?
     
  18. bbvrdev

    bbvrdev

    Joined:
    Aug 11, 2009
    Posts:
    221
    Oh man!!! I got it!! Yahooooooo :) Been working on this for days!

    Gentlemen, thanks so much for your help. It's really cool of you to jump into my problem like this. I hope I can get good enough to pay it forward one of these days.

    For anyone else with the same problem, here's the code that finally worked (thanks to dreamora):

    Code (csharp):
    1.  
    2. var temp : GameObject;
    3. temp = GameObject.Find("CoreRoot2");
    4.  
    5. someScript = transform.parent.gameObject.GetComponent(gameLogic) as gameLogic;
    6.  
    7. someScript.test();
    8.    
    9.  
     
  19. bbvrdev

    bbvrdev

    Joined:
    Aug 11, 2009
    Posts:
    221
    In fact, all I needed was this:

    Code (csharp):
    1.  
    2.  
    3. someScript = transform.parent.gameObject.GetComponent(gameLogic) as gameLogic;
    4. someScript.test();
    5.  
    6.  
     
  20. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    sounds like you have twice the same object in then so it has actually retrieved the wrong one (the one without the component)
     
  21. gdebojyoti

    gdebojyoti

    Joined:
    Mar 3, 2013
    Posts:
    28
    Thanks a lot. This helped me with my own issue. :)
     
  22. leukk

    leukk

    Joined:
    Jun 9, 2019
    Posts:
    1
    10 years later I was having the same problem :) thanks for the help
     
  23. ggeven

    ggeven

    Joined:
    May 21, 2019
    Posts:
    1
    Just a reminder. You have to find the gameobject in a MonoBehaviour Class otherwise, it does not work.