- Home /
How do I access a variable declared outside a class?
right now im doing
#pragma strict
var objects = [];
function Start () {}
function Update () {}
class objHandle {
var id : int;
var updatelist : Array;
function objHandle(position,velocity,shapestr,localscale) {
this.id = objects.length;
this.updatelist = new Array();
objects.push(this);
}
}
and I get Unknown identifier: 'objects'.
Answer by Eric5h5 · Nov 28, 2012 at 09:32 PM
The "objects" variable is not accessible to your objHandle class since it's completely unrelated. There's no way to access it unless you make it part of the objHandle class.
Some additional things: you must always define the type of variables, either explicitly or by supplying a value. Therefore "var objects = []" will not work since there is no type available. Same goes for "function objHandle(position,velocity,shapestr,localscale)". Also, do not use the Array class, it's slow and obsolete. For dynamic arrays, use a generic List.
but then how can I access things like Time.time? isnt it outside the class and thus completely unrelated?
Time.time comes from the Unity engine, not a user defined class.
$$anonymous$$ore correctly (and possibly pointing to a solution), Time.time is a static member of the Time class, meaning you can access it anywhere.
class Time { static var time : float; }
yeah, I made a static class called Globals, it works
Your answer
Follow this Question
Related Questions
Access function (and variable) outside class in the class in Javascript. 1 Answer
Accessing another script from a class 3 Answers
How to add javascript classes? 1 Answer
Construct class with enum parameter (javascript) 0 Answers
Is it possible to create a script dinamic like animation>size controling the Elements variables? 1 Answer