How to disable / enable a component in a script. (enabled)

1 minute read

Introduction

Here, the technical part is written so that beginners can understand it somehow.

enabled

Simply put, it’s a variable that allows you to enable or disable components that can be attached to GameObjects.

“shot” 2020-09-03 18.43.58.png

It’s the inspector view part like the image.
Components can be added with “Add Component”.

For example, to delete the “collider” component at the moment of execution, write like this in a script.

void Start(){
        var colliderTest = GetComponent<Collider>();
        colliderTest.enabled = false;
}

If you explain in parentheses of “void Start” of this script in order from the top

Create a variable called “colliderTest”
Put the “Cllider” component of the game object with this script in it with “GetComponent ".

Finally, “colliderTest.enabled = false;”
Since “colliderTest” is the variable created earlier,
“Enabled = false” means that it will be disabled.

vice versa

colliderTest.enabled = false;

You can also enable the component by changing the “false” part of the to “true”.

colliderTest.enabled = true;

After the change.

I will post the official Unity reference for the time being

About Unity Official Reference enabled

Also, this time it was a method to enable / disable the component attached to the game object, but there is also a method to enable / disable the “game object itself”.
I use “Set Active”, but that’s all for today.
I will post the official reference for SetActive.

Unity Official Reference SetActive