how to access gameobject in unity script code example
Example 1: how to find gameobjects in unity
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public GameObject hand;
void Example()
{
hand = GameObject.Find("Hand");
hand = GameObject.Find("/Hand");
hand = GameObject.Find("/Monster/Arm/Hand");
hand = GameObject.Find("Monster/Arm/Hand");
}
}
Example 2: findobject getcomponent
GameObject.Find("Name of the object you want to access").GetComponent<Name of the Component (Transform,Script,RigidBody,etc..)();
An Example (easy to understand):
GameObject Player = GameObject.Find("Player");
PlayerController PlayerControllerScript = Player.GetComponent<PlayerController>();
PlayerControllerScript.run = true;
Another Example:
GameObject.Find("Player").GetComponent<PlayerController>().run = true;
Example 3: how to reference this gameobject unity
this.SetActive(true);