Finding a GameObject by C# script in unity

 There are main three ways of finding a gameObject in scene by script

GameObject.Find() mehod

You can pass string name in it and it will then search for that name in scene gameObject and return very first name which exactly match with it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    private GameObject player;
    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.Find("main_character");
        Debug.Log(player);
    }
}


In this example code we have declare a private GameObject and then we assign it by calling find() method. if there is exact same name gameObject in hierarchy then it assign and show name in debug message or otherwise show a null message.
GameObject.Find("main_character");  whatever string we pass in Find() method should be same means upper case, lower case or even spaces between them.

if you want to search in a particular child of a parent then you can define pass in string by using '/' to define path for example you can use GameObject.Find("/main_character/hand") then it will only search in the child objects of main_character. It is very useful in improving performance of a game if you have too many gameObjects.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    private GameObject player_head;
    void Start()
    {
        player = GameObject.Find("/main_charater/head");
        Debug.Log(player_head);
    }
}


GameObject.FindWithTag() method

 This method search only gameObject which have a tag that you pass.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    private GameObject character;
    // Start is called before the first frame update
    void Start()
    {
        character = GameObject.FindWithTag("Player");
        Debug.Log(character);
    }
}


If tag that you passed in FindWithTag method is not defined then it will return an error Tag : Name is not defined. and if gameObject with that tag is not present then it will return an null and find gameObject with that tag then it will reference to that gameObject.
If more then one gameObject have same tag that you use in find method then it will only return any one of them.

GameObject.FindObjectsWithTag()

If you have many gameObjects with same tag and you want to assign all of them, then you can declare an array of gameObjects and call this function.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    private GameObject[] enemies;
    // Start is called before the first frame update
    void Start()
    {
        enemies = GameObject.FindGameObjectsWithTag("Enemy");
        for(int i=0;i<enemies.Length;i++)
        {
            Debug.Log(enemies[i]);
        }
    }
}

 
In this example if we want to reference to all of all enemies for that we declared a array of gameObject and call this method in start method.
enemies = GameObject.FindGameObjectsWithTag("Enemy"); will assign all gameObjects that have tag 'Enemy' .


Important points
  • You must use these find method in Awake() or a Start() method because these methods are resource consuming. and assign them with vareiables for further use.
  • You should not call these functions in Update() method at each frame.
  • Naming for finding should same example "player" & "Player"  both are difference because of lower and upper cases.
  • spaces are allowed like you can find "main character" with gameObject named "main character"
  • Space are also matter like "character " & "character" are different because first one gave an extra space in last. 
  • All these functions only search in active gameObjects so if name or tag exactly match but if gameObject is deactivated (not active) then it will return a null object. 


Popular posts from this blog

Positioning GameObjects in unity editor

Creating count down timer in unity by c# script

Changing Image in unity by C# script