- Home /
How can I extend a component?
Is it possible to extend a component in order to be attachable to GameObjects? Something the following does not work since Unity don't know about the derivation:
class mycomponent:LineRenderer { ... }
... ...
GameObject p;
p.AddComponent ("mycomponent");
...
Answer by Molix · Feb 23, 2010 at 09:28 PM
The scripts all extend MonoBehaviour. You could achieve something like what you're doing by extending MonoBehaviour, and then at runtime adding the component you want (similar to what you have, e.g. AddComponent("LineRenderer")), and then manipulate the parameters of the LineRenderer in your script.
e.g.
LineRenderer lineRenderer = null;
void Start() { lineRenderer = GetComponent(typeof(LineRenderer)) as LineRenderer; if( lineRenderer == null ) { lineRenderer = AddComponent(typeof(LineRenderer)) as LineRenderer; }
// do stuff with lineRenderer or in Update() or whatever }
I hope that helps.
I would like to do similar things too. A concrete example would be extending the configurablejoint to remove unnecessary features in the inspector GUI and add others on top.