How do I enable/disable components of my gameobject?

I am making a game where I want to enable the player to be able to pick up a shield. I want my script to enable the “halo” component when the player collides with a shield object, but I cannot figure out how to access and enable/disable the component. How do I do this?

This is how far I have gotten:

if (triggerCollider.tag == "Shield Item") {
            lives = lives + 1;
            Destroy(triggerCollider.gameObject);
            Component halo = gameObject.GetComponent("halo");
            //how do I enable/disable the effect?
        }

Thank you in advance!

A monobehaviour has property called enabled so you can just set that to true or false. In your case, you could just add the line
halo.enabled = true;
to enable it

Hey thare is alot of ways how you can do it but i will go with.
1)In editor add your “halo” script on game object and disable it.
2)In your script what is attached on game object when you want just use this.

  • In your case you must have script halo attached to gameobject where is your script with collision
  gameObject.GetComponent<halo>().enabled = true;

I get the error: ‘Halo’ is inaccessible due to its protection level (I am trying to access the default halo component that comes with unity). Is there a way around this?