How to enable and disable this option only, not the whole object with C# ?
1 Answer
If I understand correctly you're trying to achieve is to disable just a component and not the whole gameobject.
You can get any component (on the current object) using
var component = GetComponent<Type>();
Where Type
is the component you want to get. Examples in your case:
var camera = GetComponent<Camera>();
var canvas = GetComponent<Canvas>();
Here GetComponent
is called on the current gameObject where your script is, but you can call it on another gameObject, it's a public function.
Then all components can be enabled/disabled:
component.enabled = false; // Disable
component.enabled = true; // Enable
// Examples
camera.enabled = true;
canvas.enabled = false;
Finally, notice that the checkbox only appears in the inspector next to a component if some methods are found by Unity (usually: Start
and Update
).
-
1\$\begingroup\$ API reference: docs.unity3d.com/ScriptReference/Behaviour-enabled.html \$\endgroup\$ Mar 16, 2017 at 7:52