x


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);
    }
}
more ▼

asked Sep 06 '11 at 06:28 AM

yatayata gravatar image

yatayata
31 1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You need to use OOP and extend ScriptableObject.

class PathMaker extends ScriptableObject {
  // my code
}
more ▼

answered Sep 07 '11 at 05:59 AM

Tyler 10 gravatar image

Tyler 10
88 3 3 7

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?

Sep 08 '11 at 12:23 AM yatayata
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x204
x67
x36

asked: Sep 06 '11 at 06:28 AM

Seen: 1138 times

Last Updated: Sep 08 '11 at 12:23 AM