How to enable and disable components through a script?

I want to be able to drag and drop a component onto my script in the Inspector like a BoxCollider or a Script, and then turn it on or off when StartAction() is called.
I tried this with .enabled = false or .SetActive(false) but then it says Component has no definition for these options.

public Component _actionObject;
  
public void StartAction()
{
      _actionObject.enabled = false;
}

For now I solved it temporarily by adding a sub GameObject to the main GameObject this script is attached to, which would allow me to disable the Box Collider or script added to the sub-gameobject I want to disable by running _actionObject.SetActive(false), but for memory space reasons having al these extra gameobjects isn’t the optimal solution.

public GameObject _actionObject;
  
public void StartAction()
{
    _actionObject.SetActive(true);
}

Can someone please help me figure this out? :slight_smile:

If you want to enable or disable a Box Collider, you can pass a bool parameter to StartAction method and based on that bool parameter, you can enable or disable your Box Collider

public BoxCollider _actionObject;
  
public void StartAction(bool e)
{
    if(e)
        _actionObject.enabled = true;
    else if(!e)
        _actionObject.enabled = false;
}

If you want to enable or disable your script, you need to write name of your script. Let’s say I have created a script with the name TestScript then I can write

public TestScript _actionObject;
  
public void StartAction(bool e)
{
    if(e)
        _actionObject.enabled = true;
    else if(!e)
        _actionObject.enabled = false;
}

Not sure if you have read the comments(below your question), but two comment/answers were given. But as was stated, you need to get the component from the gameObject, store it as a variable, then tell the variable to be enabled or not. As you show in the second part of your question, you have found that calling the gameObject works.

As silvervidia has shown, you could go the route of declaring the specified component as a Behaviour, as well.

But if you wish to use the route of my suggestion, and plan on copy pasting code and may have different components in each object? You will need each gameObject to have the same components in them(or else errors will occur), and just turn them to .enabled = false; within the Awake() function of the appropriate script.

But as all have stated, you cannot declare a Component as a variable, the way you are trying to do it. Using a Component as a variable only works through functions as Component; or by making each object declare it’s named variable component, and calling it that way.

But if you store classes instead of gameObjects, as your references from other classes, it is as easy as saying:

class2.renderer.enabled = false;  
class3.rigidbody.isKinematic = true;  
class4.collider.enabled = true;  
class5.StartNewFunction(true);  
/// etc...

I wish Unity would implement something as basic as this. This would save me so much time

Having read your replies to other responses, I think the way to solve your problem would be something like the following:

public bool disableBoxCollider;
public bool disableScript;
public bool disableSpriteRenderer;
// etc.

public void Disable()
{
    if (disableBoxCollider) {
        gameObject.GetComponent<BoxCollider>().enabled = false;
    }
    if (disableScript) {
        gameObject.GetComponent<InsertScriptNameHere>().enabled = false;
    }
    if (disableSpriteRenderer) {
        gameObject.GetComponent<SpriteRenderer>().enabled = false;
    }
}

Basically, you can specify what you want disabled in the inspector (or through the script itself) and when the function is called, all of the things set to be disabled will be disabled.
Hope this helps!

I want to do what @wideeyenow_unity says and I want to set circlebody2d"s is trigger function tru how can ı do that ı just tried putting

class2.CircleCollider2D.IsTrigger = true;

in void Awake but it doesn’t seem to understand what class2 is and i don’t know how to make the thing a variable.