- Home /
Casting to a Type using a String
Hello, I simply want to fetch a component from a gameobject and cast it using the AS-operator to the proper type. I do know the name of the type on runtime only and that name is stored in a string.
Using the type directly in code works fine:
go.GetComponent(configuration) as Config1; // works
But as I pointed out, I don't know about the type before runtime. All I have is a string and obviously this does not work:
go.GetComponent(configuration) as "Config1"; // can not work
I tried to use getType to convert the string to the correct type like so:
go.GetComponent(configuration) as Type.GetType("Config1"); // compile error
That results in the following error in Unity Editor: Unexpected symbol `(' (but no Error in MonoDevelop)
So, how do I convert the String "Config1" to a valid casting information?
Greets
What do you plan on doing with the value after you've cast it to an unknown type? Unless all of your unknown types have a common interface you won't be able to call methods on it, so the type cast isn't giving you anything. If they have a common interface then you can just cast it to the interface, or to the ancestor class, and call virtual methods.
. +1 agree with @Dave Carlile. Similar to using eval()
in JavaScript, or using dynamic SQL statements, patterns like this are typically a symptom of an incorrect design decision somewhere...
Thanks for the suggestions. $$anonymous$$aybe it's just me diving into C# just recently and it's a matter of how to address inheritance correctly. Let me explain a little more in detail what I try to achieve.
$$anonymous$$y project will be compiled to different platforms in different versions (think paid and free version). For each build of the game there are little things to be differently configured. Depending on platform/version features need to be disabled, others need to be activated in different manners, e.g. Gamecenter on iOS only.
Now I thought it would be clever to check platform/version in the first scene and then attach one of a few different scripts with all configuration-information to be used throughout the game. That configuration-class inherits from an abstract Singleton BaseConfiguration.cs and is attached to a GameObject in the Scene tagged "Configuration".
Now, those files/classes are named like "ConfigAppleAppstorePaid.cs", "ConfigGooglePlaystoreFree.cs", etc and of course share fields and method names from the abstract BaseConfiguration.cs.
I do know the name of the script to be a attached to the "Configuration"-GameObject from my evaluation on game-start and I know it inherits from BaseConfiguration.cs. However I have trouble to achieve the goal to fetch that configuration from the GameObject again and cast it properly as all I have is the name of the class as string at that point.
I am sure there is some way to use the BaseConfiguration-Type as Casting-Target but I tried different variations and did not succeed. So maybe it's the syntax or maybe the idea how to solve my problem is not a clever one in the first place.
Unless you are going to make 2 separate versions of the game (one free, limited and one paid, full version) It's not going to work the way you want. Tricking the game into thinking it's the full version isn't that difficult for some people.
Security as in preventing piracy is not my concern in this case. If it was, your point would be valid of course.
Answer by Dave-Carlile · Jun 10, 2015 at 11:33 AM
Use GetComponent < YourBaseType >()
. Create a virtual method(s) in the base type that can do the config specific handling, override those methods in the child classes. Always use the virtual methods to do platform specific things depending on the config object instance.
Regardless, at some point in your code you'll need specific code to handle the specific differences such as different config screens, different code paths.
Another option for that sort of thing is to use conditional compilation, which might be better for your needs.
Excellent - thank you, that did the trick for me. It was just a matter of using GetComponent with the right BaseType. Thanks again!
Answer by dubbreak · Jun 09, 2015 at 08:29 PM
You need to look up reflection. Convert.ChangeType() in specific.
The problem with using reflection in Unity is it isn't supported by all platforms (to the best of my knowledge).
If it's just a case of proper subtyping and you don't have many types, you could cheat and use a switch statement (switch on the string and in each case do a normal cast).
Dave has a good point. What you are trying to do may not be necessary. Why do you think you need to cast? It might not be the best solution for the problem you are trying to solve.
I have elaborated in the first answer a little more in detail what I try to achieve. But yes, a switch would work though it is not very elegant in code. But nevertheless thank you for that suggestion. Unless I change my whole approach because it is not clever, that is probably the way to go.
I believe ChangeType only allows converting between CLR types such as int, single, string, etc.