How to know if a GameObject is ACTIVE or INACTIVE in Unity

Introduction

GameOb­jects are the ele­ments of the scene that are used to mod­el every­thing that exists in our project in Uni­ty. One of the basic prop­er­ties that these GameOb­jects have is their acti­va­tion state, in the inspec­tor win­dow can be seen as a check­box that when checked the GameOb­ject is ACTIVE and if the check­box is unchecked the GameOb­ject is INACTIVE.

Dear read­er

🍷🧐

In this arti­cle we are going to see how to know the state of any GameOb­ject through code in Unity.

ABOUT THIS VIDEO

In this video we see how to know if a GameOb­ject is ACTIVE or INACTIVE in the scene in Uni­ty through code

HERE MY UNITY PLAYLIST
👇🏽

Procedure to know if a GameObject is ACTIVE or INACTIVE in the scene

Let's assume that there is already a script cre­at­ed and assigned to a GameOb­ject in Uni­ty so that its instruc­tions can be executed.

Step 1 — Define REFERENCE

To solve prac­ti­cal­ly any prob­lem in Uni­ty we have to start with the vari­ables that we are going to use, in this case we need to have the ref­er­ence of the GameOb­ject that we want to ana­lyze, in oth­er words, in our script we will define a glob­al vari­able of type GameOb­ject, for exam­ple in the fol­low­ing way:

pub­lic GameOb­ject myGameObject

Step 2 — Initialize REFERENCE

The vari­able we defined in the pre­vi­ous step will not auto­mat­i­cal­ly point to the GameOb­ject we are inter­est­ed in know­ing if it is ACTIVE or INACTIVE in the scene, we have to make sure that happens.

There are sev­er­al ways to ini­tial­ize a vari­able, click here to see a playlist with sev­er­al videos where you can see meth­ods and exam­ples on this top­ic, in this case we are going to go to the inspec­tor where we have the script assigned and we are going to drag man­u­al­ly the GameOb­ject that we are inter­est­ed in ana­lyz­ing to the GameOb­ject type vari­able that appears in the inspec­tor, in our case it is called "myGameOb­ject". This way now that vari­able will point to the GameOb­ject that we are inter­est­ed in.

Step 3 — How to READ the GameObject status

Hav­ing solved the pre­vi­ous two steps we can now use the vari­able we defined to know if the GameOb­ject is ACTIVE or INACTIVE in the scene. For this we use the fol­low­ing instruction.

myGameObject.activeInHierarchy

The pre­vi­ous instruc­tion is equiv­a­lent to a boolean val­ue, if the GameOb­ject to which the vari­able "myGameOb­ject" is point­ing is active in the scene the expres­sion will give as result "true", while if it is inac­tive it will give as result "false". There­fore we can use this expres­sion to do what­ev­er we want, for exam­ple define an IF state­ment and print a mes­sage in con­sole if the object is active and a dif­fer­ent mes­sage if it is inac­tive, for example:

if(myGameObject.activeInHierarchy){
Debug.Log("The GameOb­ject is ACTIVE");
}else{
Debug.Log("The GameOb­ject is INACTIVE");
}

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

Scroll to Top
Secured By miniOrange