- Home /
Enable/Disable a Component by using a String Variable for the Component name
Hi, I need to Enable a component by string.
i am trying to this : go.GetComponent<myComponent>.Enabled = true;
but i want to be a variable so now i am getting my component like this :
aComponent = go.GetComponent(componentNameString);
But i can't do :
aComponent.Enable = True;
the 'componentNameString' is given by the user in the inspector that's the main reason why i need to use a string.
There's no problem with the code you posted, except what is probably a typo (you missed the 'd' at the end of enabled:
//Correct
aComponent.Enabled = True;
//Wrong
aComponent.Enable = True;
However, if I understand well, I think that the real problem is that you have to specify the name of the component before compilation takes place, so that the compiler knows what to do with it, but you want to specify it at run time. What I don't understand is how a "user" will have access to the Inspector.
$$anonymous$$AYBE this can be done through Reflection (or maybe not), but I've never had the need to use Reflection, so I can only tell you to check C#/Reflection to see if it can be done, or maybe someone who has expertise with Reflection can help you out.
Hi, Both do not work and i also tried using reflection but thanks anyway to try :)
I'm pretty sure it should be lowercase .enabled = true;
This is strange! Anyway, I'm not 100% clear on what you're trying to do. $$anonymous$$aybe if you give some more detail, it'll be easier to focus on the problem at hand.
Answer by Bunny83 · Jul 30, 2017 at 03:12 AM
A Component doesn't have an "enabled" property. Only some specific derived classes have one. For example your own scripts get their enabled property from the Behaviour class. The MonoBehaviour class is derived from Behaviour. So if the components you want to enable / disable are scripts (components derived from MonoBehaviour) or other components derived from Behaviour you can simply cast the result of GetComponent to "Behaviour". This allows you to use it's enabled property.
Be careful that when the actual component that is returned can't be casted to Behaviour an exception would be thrown.
Have a look at this class hierachy(it's old but the core classes are there). As you can see even many built-in components are derived from Behaviour. However there are some components that also have an enabled property but are not derived from Behaviour (like Renderer and all it's child classes).
To cover most components you can do it like this:
// C#
var comp = go.GetComponent(componentNameString);
Behaviour be = comp as Behaviour;
if (be != null)
{
be.enabled = true;
}
else
{
Renderer rend = comp as Renderer;
if (rend != null)
rend.enabled = true;
}
Another way might be to use reflection to search for an actual property with the name "enabled". However reflection can cause many problems if not used properly and is generally slow. Though it would allow to set the "enabled" property of any kind of component.
Hi. Thanks this seems to work for most components, the only ones that i found so far are colliders. But i can live with that, as there are already specific actions for Enable colliders
here is a copy from the (Playmaker) action script.
using UnityEngine;
namespace HutongGames.Play$$anonymous$$aker.Actions
{
[ActionCategory(ActionCategory.GameObject)]
[Tooltip("Destroys a Component of an Object. WARNING! COLLIDERS DO NOT WOR$$anonymous$$ WITH THIS ACTION and Some Components $$anonymous$$ight not work!")]
public class ActivateComponent : FsmStateAction
{
[RequiredField]
[Tooltip("Place Component Object here n/WARNING COLLIDERS DO NOT WOR$$anonymous$$!!")]
public FsmObject component;
[Tooltip("Activate / Deactivate the Component")]
public FsmBool Activate;
public override void Reset()
{
component = null;
Activate = false;
}
public override void OnEnter()
{
DoActivateComponent();
Finish();
}
void DoActivateComponent()
{
if (component.Value == null)
{
LogError("No Component Selected");
Debug.Log("No Component");
}
else
{
Behaviour be = component.Value as Behaviour;
if (be != null)
{
be.enabled = Activate.Value;
}
else
{
Renderer rend = component.Value as Renderer;
if (rend != null)
rend.enabled = Activate.Value;
}
}
}
}
}
Well you can just add another case for the Collider base class to support all colliders as well. In addition you can add a "last resort" fallback using reflection. That way it would be the most versatile. I also would use early-exits in this case. Something like this:
void DoActivateComponent()
{
if (component.Value == null)
{
LogError("No Component Selected");
Debug.Log("No Component");
return;
}
Behaviour be = component.Value as Behaviour;
if (be != null)
{
be.enabled = Activate.Value;
return;
}
Renderer rend = component.Value as Renderer;
if (rend != null)
{
rend.enabled = Activate.Value;
return;
}
Collider col = component.Value as Collider;
if (col != null)
{
col.enabled = Activate.Value;
return;
}
var t = component.Value.GetType();
var p = t.GetProperty("enabled", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (p != null && p.CanWrite && p.PropertyType == typeof(bool))
{
// maybe log a warning like this
//Debug.Log("ActivateComponent:: reflection fallback for class'"+t.FullName+"'");
p.SetValue(component.Value, Activate.Value, null);
return;
}
LogError("selected component doesn't have an 'enabled' property");
Debug.Log("selected component doesn't have an 'enabled' property");
}
Answer by xRinaruk · Jul 29, 2017 at 11:26 PM
I think it should works if you get all assembiles and then get all types. If you have them all, just compare to string and you will have the one you are looking for. Then you can use GetComponent and set enable/disable.
Your answer
Follow this Question
Related Questions
How to select BoxColliders of children and disable them? 1 Answer
Change variable of ex2D Sprite using the component ExScreenPosition via code .js script 0 Answers
Accessing a variable of a script from another scene. 7 Answers
How to deactivate something when holding down a key? 1 Answer
enable disable variable of a script from another script 4 Answers