- Home /
Disable an unknown type of component at Runtime
I would like to disable a specific component. It seems easy but the type of the component is dynamic, that is a public variable that set on the Unity inspector.
My code:
public class QualityControl : MonoBehaviour {
public int AtMinQuality;
public Component myComponent;
void Start () {
if(QualitySettings.GetQualityLevel()<=AtMinQuality){
//myComponent.enabled = false; <--dont work
//error CS1061: Type `UnityEngine.Component' does not contain a definition for `enabled'...
//Component.Destroy(Componente); <--work but i want disable it, not delete!
}
}
In simple terms, if my quality setting is less a specific number, i want to disable the component set in the inspector... I can? How?
I try with this code to get the type of my component:
myTipe=myComponent.GetType();
GetComponent<myTipe>().enabled = false; <--dont work
return this error: error CS0118: QualityControl.myTipe' is a
field' but a `type' was expected
Where myTipe is a System.Type variable.
Any ideas?
(sorry for my bad english)
Answer by whydoidoit · Mar 15, 2014 at 03:07 PM
Don't define it as Component - things that can be disabled inherit from Behaviour so declare it as that.
If possible i would like to avoid changing the declaration of the component...
I try: ((Behaviour)GetComponent()).enabled = false; In case i want disable the Bloom effect. And work... but, if i cant know the tipe of my component?
myType=$$anonymous$$yComponent.GetType(); ((Behaviour)GetComponent()).enabled = false;
Dont work. Return the error CS0118: QualityControl.myTipe' is afield' but a `type' was expected
I can't read your code properly, don't use angle brackets unless it is an indented code block - it makes you point hard to follow right now.
Answer by WILEz75 · Mar 18, 2014 at 07:58 PM
Solved. Thank you! I simply change Component with Behaviour. No lost the connections on the object in the inspector... (If you have time, read your forum of unity serializer... i have a little problem.)