Prefabs, Classes and GameObjects. I don't understand.

Hello,

I’m still trying to get my head around Unity’s approach to OOP. Seems easy but I’m very confused with the notion of instantiating prefabs when you bring classes into the mix.

Here’s my example.

I have a prefab of a planet and its pivot. The Planet gameObject has a meshFilter as a component which is just a sphere as well as a meshRenderer.

[9019-screen+shot+2013-03-17+at+7.38.07+pm.png|9019]

I’ve created a script called Planet and added this as a component to the Planet GameObject. Here’s the code.

#pragma strict

public class Planet extends MonoBehaviour { 
	
	var planetName : String;//name
	var planetObject : GameObject;
	
	function Planet(name : String){
		planetName = name;
		planetObject : gameObject
	}
	
	function Start () {
	}
	
	function Update () {
	}
}

To run things I’ve got an empty GameObject called Main and I’ve added a script component to it. Here’s the code.

#pragma strict

public var planet : Planet;

function Start () {
	planet   = new Planet("Earth");
}

function Update () {
}

What I’m trying to do is create an instance of the Planet prefab (and the planetPivot) but I’ve completely confused myself. I know I should be using instantiate but how does that work with classes? Should I be using classes at all? I want to use a class for inheritence.

Thanks in advance!

just FYI. if you want, just don’t use prefabs. I find them annoying and don’t use them often.

var newGameObject:GameObject;
newGameObject = Instantiate( your model here );

just FWIW some discussion about making a pool , it has many Instantiate examples

I’m still trying to get my head around
Unity’s approach to OOP.

This is because most Unity documentation completely ignores it. The notion of manually dragging simple things into a “scene” is heavily favored for some reason, instead of how to program a functional game.