- Home /
creating instances
I want to create my own classes and instantiate like normal OOP code.
I tried using
line = ScriptableObject.CreateInstance(PathMaker);
93[ScriptableObject.CreateInstance() ][1]93 but this gave:
Instance of PathMaker couldn't be created.
The the script class needs to derive from ScriptableObject.
how should i do that from javascript?
my class looks something like the below. its just to draw lines on screen, but i need a lot of instances of these paths with their own points.
// Creates a line renderer for path of objects
var c1 : Color = Color.yellow;
var c2 : Color = Color.red;
var path: Array;
var lineRenderer: LineRenderer;
function Start() {
lineRenderer = gameObject.AddComponent(LineRenderer);
lineRenderer.material = new Material (Shader.Find("Particles/Additive"));
lineRenderer.SetColors(c1, c2);
lineRenderer.SetWidth(0.2,0.2);
var points = [Vector3( 0,0,0),
Vector3(0,50,0)
];
setPath(points);
addPoint(Vector3(0,10,0));
}
public function setPath(pointList: Array) {
path = pointList;
setPointCount();
}
public function addPoint(pt: Vector3) {
path.Push(pt);
setPointCount();
}
function setPointCount() {
lineRenderer.SetVertexCount(path.length);
}
function Update() {
var lineRenderer : LineRenderer = GetComponent(LineRenderer);
for(var i : int = 0; i < path.length; i++) {
var pos: Vector3 = path[i];
lineRenderer.SetPosition(i, pos);
}
}
Answer by Tyler 10 · Sep 07, 2011 at 05:59 AM
You need to use OOP and extend ScriptableObject.
class PathMaker extends ScriptableObject {
// my code
}
thanks for the tip. in the end i think i need to do it with attaching a script to an object in the Unity GUI because of this line:
lineRenderer = gameObject.AddComponent(LineRenderer);
ie ScriptableObject doesnt have access to a gameObject?
just to confirm: something starting with lowercase is an instance in Unity, correct? translation
is an instance whereas Translation.foo()
calls a static method of Translation class, which can return a singleton?