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): Application.LoadLevel("GameLevel2"); Map map = GameObject.Find("Map").GetComponent<Map>(); 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.
Wait a minute, no its not! Code (csharp): function Update () { GameObject.Find("Test").GetComponent("Test2").go = true; if (GameObject.Find("Test").GetComponent("Test2").go) Debug.Log("Works!"); } Object Reference not set to instance of object Code (csharp): function Update () { gameObject.Find("Test").GetComponent("Test2").go = true; if (gameObject.Find("Test").GetComponent("Test2").go) Debug.Log("Works!"); } 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().
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
You have to store it in a variable such as Code (csharp): using UnityEngine; using System.Collections; public class example : MonoBehaviour { public GameObject hand; void Awake() { hand = GameObject.Find("Hand"); hand = GameObject.Find("/Hand"); hand = GameObject.Find("/Monster/Arm/Hand"); hand = GameObject.Find("Monster/Arm/Hand"); } } http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html