find object unity code example

Example 1: how to find gameobjects in unity

using UnityEngine;
using System.Collections;
// This returns the GameObject named Hand in one of the Scenes.
public class ExampleClass : MonoBehaviour
{
    public GameObject hand;    
    
    void Example()
    {
        // This returns the GameObject named Hand.
        hand = GameObject.Find("Hand");        
      
        // This returns the GameObject named Hand.
        // Hand must not have a parent in the Hierarchy view.
        hand = GameObject.Find("/Hand");     
      
        // This returns the GameObject named Hand,
        // which is a child of Arm > Monster.
        // Monster must not have a parent in the Hierarchy view.
        hand = GameObject.Find("/Monster/Arm/Hand");
        
        // This returns the GameObject named Hand,
        // which is a child of Arm > Monster.
        hand = GameObject.Find("Monster/Arm/Hand");
    }
}

Example 2: freeze object unity

public Rigidbody2D rb;

void Start()
{
	//Freeze Rotation on X Axis
	rb.constraints = RigidbodyConstraints2D.FreezeRotationX;
    //Freeze Rotation on Y Axis
    rb.constraints = RigidbodyConstraints2D.FreezeRotationY;
    //Freeze Rotation on Z Axis
    rb.constraints = RigidbodyConstraints2D.FreezeRotationZ;

	//Freeze Position on X Axis
	rb.constraints = RigidbodyConstraints2D.FreezePositionX;
    //Freeze Position on Y Axis
	rb.constraints = RigidbodyConstraints2D.FreezePositionY;
    //Freeze Position on Z Axis
	rb.constraints = RigidbodyConstraints2D.FreezePositionZ;
  
    //Freeze All Axis (Rotation & Position)
    rb.constraints = RigidbodyConstraints2D.FreezeAll;
    //UnFreeze All Axis (Rotation & Position)
    rb.constraints = RigidbodyConstraints2D.None;
}

Example 3: unity find gameobject

ExampleObject = GameObject.Find("Hand");

Example 4: unity find object by name

using UnityEngine;
using System.Collections;// This returns the GameObject named Hand in one of the Scenes.public class ExampleClass : MonoBehaviour
{
    public GameObject hand;    void Example()
    {
        // This returns the GameObject named Hand.
        hand = GameObject.Find("Hand");        // This returns the GameObject named Hand.
        // Hand must not have a parent in the Hierarchy view.
        hand = GameObject.Find("/Hand");        // This returns the GameObject named Hand,
        // which is a child of Arm > Monster.
        // Monster must not have a parent in the Hierarchy view.
        hand = GameObject.Find("/Monster/Arm/Hand");        // This returns the GameObject named Hand,
        // which is a child of Arm > Monster.
        hand = GameObject.Find("Monster/Arm/Hand");
    }
}

Example 5: unity gameobject.find

GameObject obj = GameObject.Find("/Parent/Child/ChildOfChild");

Example 6: what does gameobject.find return

//It returns null/void