Dynamic Type Casting
I could use some help on this one. Is it possible to dynamically type cast something? Here is what I am doing:
GameObject holder;
holder = (string.IsNullOrEmpty(action.tagOfHolder) == false) ? GameObject.FindGameObjectWithTag (action.tagOfHolder) : GameObject.Find(action.nameOfHolder);
if (holder.GetComponent (action.playerComponentScript) != null) {
Type mytype = Type.GetType (holder.GetComponent (action.playerComponentScript));
mytype script = holder.GetComponent (action.playerComponentScript);
I keep getting this error:
The type or namespace name `mytype' could not be found. Are you missing a using directive or an assembly reference?
I don't know the types of the component since this is decided based on what I am trying to find. I want to dynamically type cast this so I can enable/disable the component. I can't just do:
holder.GetComponent (action.playerComponentScript).enabled = true;
because it needs to know what type it is(Example):
holder.GetComponent ("Animation" as Animation).enabled = true;
Anyone have any direction for me?
NOTE: Other variations I have tried...
(holder.GetComponent (action.playerComponentScript) as myType).enabled = true;
(mytype)holder.GetComponent (action.playerComponentScript).enabled = true;
it's more a question of who has the enabled ability. that's $$anonymous$$onoBehaviour. use that ins$$anonymous$$d.
@hexagonius Unfortunately, that was one I have tried previously and it didn't do the trick for me :(.
Oh my goodness! @PizzaPie you are a life saver. I took what you said and did the following:
GameObject holder;
holder = (string.IsNullOrEmpty(action.tagOfHolder) == false) ? GameObject.FindGameObjectWithTag (action.tagOfHolder) : GameObject.Find(action.nameOfHolder);
if (holder.GetComponent (action.playerComponentScript) != null) {
Type mytype = holder.GetComponent (action.playerComponentScript).GetType ();
if (mytype.GetProperty("enabled") != null)
{
System.Reflection.PropertyInfo rpi = mytype.GetProperty("enabled");
rpi.SetValue(holder.GetComponent (action.playerComponentScript), action.scriptEnabled, null);
}
....
That worked like a charm! You need to put your comment as an answer so I can mark it as correct. This is amazing, thank you.
Answer by PizzaPie · Feb 03, 2017 at 04:25 PM
does it work till Line 5? if so use that
public static void SetEnabledTo(System.Object targetObj,bool isEnable)
{
if (targetObj == null)
return;
Type aType = targetObj.GetType();
if (aType.GetProperty("enabled") != null)
{
System.Reflection.PropertyInfo rpi = aType.GetProperty("enabled");
rpi.SetValue(targetObj, isEnable, null);
}
}
Note:According to c# docs ProperyInfo.SetValue has an overload Set(Object,Object) but for some reason it won't show it on VS
or change the mytype on line 6 to var but you ll still won't know if enabled propery exist
Your answer
Follow this Question
Related Questions
How to add modding/scripting support? 0 Answers
Dynamically Loading Unity Content Without Publishing App Store Update 1 Answer
How to create Mecanim animation in script from scratch? 0 Answers
Why does my Override Controller dosen't change the Animation Clip? 1 Answer
Issues running gdb debug mode in runtime,Running GDB during run time of application 0 Answers