Search Unity

GameObject.Find() didn't work?

Discussion in 'Scripting' started by NHA_DIEN, Sep 6, 2011.

  1. NHA_DIEN

    NHA_DIEN

    Joined:
    Jul 5, 2011
    Posts:
    15
    Hi all,
    I have a game with 2 scenes: "GameLevel1" and "GameLevel2"
    In "GameLevel2" scene I have an object named "Map" and have a component(script) named "Map", too.
    I want to get Map script after changing scene from GameLevel1 to GameLevel2, and I used scripts:

    Code (csharp):
    1.  
    2. Application.LoadLevel("GameLevel2");
    3. Map map = GameObject.Find("Map").GetComponent<Map>();
    4.  
    But at runtime, I receive an exception:
    I still see the Map object, with Map script in the Hierarchy list.
    Anyone can help me?
    Thank you very much.
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Didn't you say that the map script was on the GameLevel1 and 2? You should be finding them instead.
     
  3. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Um, I think it has to be lowercase g as in

    Code (csharp):
    1.  
    2. gameObject.Find("Map")
    3.  
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    False. It's uppercase.
     
  5. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Really? I'm thinking javascript...
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    It's the same in every Unity language.
     
  7. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Wait a minute, no its not!

    Code (csharp):
    1.  
    2. function Update () {
    3.     GameObject.Find("Test").GetComponent("Test2").go = true;
    4.         if (GameObject.Find("Test").GetComponent("Test2").go)
    5.         Debug.Log("Works!");
    6.  
    7. }
    8.  
    Object Reference not set to instance of object


    Code (csharp):
    1.  
    2. function Update () {
    3.     gameObject.Find("Test").GetComponent("Test2").go = true;
    4.         if (gameObject.Find("Test").GetComponent("Test2").go)
    5.         Debug.Log("Works!");
    6. }
    7.  
    Works!

    http://unity3d.com/support/documentation/ScriptReference/GameObject.html

    GameObject is a component, which means that it can a variable newGameObject : GameObject, or you can access it, transform.gameObject. From there, you can do GetComponent() or Find().
     
  8. NHA_DIEN

    NHA_DIEN

    Joined:
    Jul 5, 2011
    Posts:
    15
    Thanks,
    I tried 2nd way, but a compile error occured. Find() is a class function (static function in C#), so you can't access it with an instance, instead of classname
     
  9. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    You have to store it in a variable such as

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class example : MonoBehaviour {
    6. public GameObject hand;
    7. void Awake() {
    8. hand = GameObject.Find("Hand");
    9. hand = GameObject.Find("/Hand");
    10. hand = GameObject.Find("/Monster/Arm/Hand");
    11. hand = GameObject.Find("Monster/Arm/Hand");
    12. }
    13. }
    14.  
    http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html