- Home /
Dynamic type, unknown in advance to reuse scripts
I'll like to implement a script that a lot of GameObject will share. The idea is that each gameobject will have then a specific script with their own methods.
The idea behind this is to have each gameobject containing diferent level stuff, like backgrounds, animations, specific functions, etc. But then have a specific gameobject with sub managers that will manage common things like menus, gui etc.
I'll like the main one to be able to load different "scenarios" without knowing which type of scirpt im loading.
I'll like to use the main script to load on demand the other one
Main:
function loadSpecificScript (n) { var sc = gameObject.GetComponent('specificScript'+n); }
The problem is that the sc var then doesnt have the correct type as I dont know it in advance.
Any help??
Answer by Knskan3 · Jun 23, 2015 at 10:48 PM
I solved it by creating an abstract class. Then I can implement specific extending classes. The good part is that u can declare a variable of type "parent Type" and then drag the child class and it will work like a charm.
Your answer
![](https://koobas.hobune.stream/wayback/20220613201203im_/https://answers.unity.com/themes/thub/images/avi.jpg)