Instantiating an Object in a Class?

What I want to do is use classes for different types of trees. Keep things organized and logically together.

So I setup my base tree class and then extend it for a specific type of tree:

 class Trees {
     var Obj : GameObject  = new GameObject("Tree");
 	var filter : MeshFilter  = Obj.AddComponent(MeshFilter);
 	var renderer : MeshRenderer  = Obj.AddComponent(MeshRenderer);

 class Sycamore extends Trees {
    function Sycamore() {
		filter.mesh = Resources.Load("Art/Meshes/Environment/Trees/Sycamore", Mesh);


Next I want to create a sycamore at a specific location (and later multiple locations using an array for the trees) and I’m not having any luck trying to instantiate it. Is there another, better way - or if instantiating is the way to go can someone provide a same line to do so?


You’re fighting against Unity’s component model.

Instead, make Tree a standalone script, not wrapped in class { ... }. That will give you a Tree Component (this is just a shorthand for extending MonoBehaviour, but ignore that for now). Then you can drag that component into your Sycamore prefab.

You probably don’t even need subclasses, since a Sycamore does not behave differently to any other tree. In all fields of computing, building a deep hierarchy is usually a mistake, a trap set by professors who use Shapes, Trees, and Cars as example hierarchies.