Hello, I’ve done some research and found that the Activator is the best way to make this possible, however, the code that they provide as an example contains a method Activator.Create(), which I cannot find with MonoDevelops intellisence, I’ve also tried typing the word which gave me an error within Unity.
So, I was wondering, is there another way to do this?
As a side note, the class will be used as a data class so that I may fetch different information dependent on the class.
My last point may have been confusing, the quintessential point is that I can pass an instance of a class which I can use to retrieve data from said class.
well, Activator.Create is a method for creating an instance of a class reflectively (using a Type parameter). And it should be there… you just need to make sure you either are using the ‘System’ namespace… or access it explicitly as ‘System.Activator.Create’.
Your question however is how to pass a class as a parameter…
And your general use case is a bit vague.
So rather than… asking how to use a method you think solves your problem, or finding an alternative to said thing you think solves your problem… how about you tell us what your problem is and we might be able to resolve that instead of dancing around the crux of the issue.
Still a bit vague. Tell us more about the context, e.g. how does the user choose the option / what kind of input will be given, who does provide the class name X, how is it associated with the input and last but not least, why?
There might be a way better, i.e. a cleaner, more performant and most importantly type-safe way to implement it.
Reflection is one of many great tools, but also often used in situations which do not require it at all.
It mostly serves the purpose to work with / access meta information or implement unusual ways of access.
I do apologise, I don’t seem to be very good at explaining this.
The option is more chosen by me, but having a very large switch statement seemed a little bit over-the-top for me.
I wanted to make this as clean as possible, making everything that I need to change, easily changeable.
I choose the option via the inspector, which I will drag and drop a script into. (Thinking about this more logically, I might not need to pass it as a parameter into a function however I will still need to have a variable that allows me to store said class.) The script will contain data that I can use as initialisation for health, name, etcetera for a character. Rather than create a new script for each character, I thought having a central script that relies on data from another script would make it much easier. (If I didn’t have this central script then I would be repeating myself over and over again with the same code.)
The class name X is provided by me, in the inspector if not already answered above.
I’m not sure what you mean with that question, or why, for that matter because they’re the same question.
Seems like you can inherit your data-providing class from ScriptableObject.
With that you can create as much asset-instances as you like, each of them defining its own specific set of data for every potential type of character. The data you want to provide will be exchangable in that you can just drop another SO instance into the slot.
If I totally missed something, feel free to correct me.
Yes you can use (serialized) variables as well.
Though SOs have the advantage that you define the data set per instance and anything that references the instance uses the data set it provides.
Any change of the SO would automatically provide the correct “new” data to everything that depends on it. It’s especially useful for an iterative improvement process in which you can test whether the data fits your needs (for example a character balancing process). This can also be achieved by having multiple prefab-instances, but why would you do that if you can break it down to the level of the data-set?
In contrast, if you expose the data on a MonoBehaviour, you may end up having mutliple instances already placed in the scene and some of them may need the same data set to start with. In order to achieve that, you’d literally have to adjust every single scene instance (unless you completely chose to instantiate everything from a corresponding prefab).
Example for that would be pre-placed NPC / monsters, which usually share a common base-data-set per species.