Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by wesleywh · Feb 03, 2017 at 02:57 AM · runtimedynamictypecasting

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;

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Feb 03, 2017 at 09:05 AM 0
Share

it's more a question of who has the enabled ability. that's $$anonymous$$onoBehaviour. use that ins$$anonymous$$d.

avatar image wesleywh · Feb 03, 2017 at 11:37 PM 1
Share

@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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

92 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges