JS OOP trouble

i have a script called skills.js :

class Skill {
	var damage: float;
	var rate : float;
	var hits : int;
	var selected : boolean;
	var name : String;

	function Skill(nam : String, dmg : float, rat : float, hit : int, sel : boolean){
		this.damage = dmg;
		this.rate = rat;
		this.hits = hit;
		this.selected = sel;
		this.name = nam;
	} 
}

punch = new Skill("punch", 5, 0.2, 1, false);
slash = new Skill("punch", 3, 0.1, 1, false);
headbutt = new Skill("punch", 8, 1, 1, false);
push = new Skill("punch", 2, 0.6, 1, false); `

and i want to access the it’s values properties in another script. for example in player.js:

var damage = skillScript.punch.damage;

but is won’t let me and it returns as a log warning when ever i start the game up. if somebody knows what i need to do please help. thanks for reading and here is the error if it helps:

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name)
UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)
player.FixedUpdate () (at Assets/Scripts/player.js:54)

glad i could help and sorry for the inconvinience again

ill add an answer to both of your questions so that you can accept them and make it easy for future visitors to identify the problem

1 Answer

1

It seems you want punch, slash etc. to be global variables. The equivalent to global in Unity is static - you must declare them this way:

static var punch = new Skill("punch", 5, 0.2, 1, false);
static var slash = new Skill("punch", 3, 0.1, 1, false);
static var headbutt = new Skill("punch", 8, 1, 1, false);
static var push = new Skill("punch", 2, 0.6, 1, false);

You can access these globals in other scripts using the script name:

  var damage = skills.punch.damage;

thanks it works like magic

That is actually incorrect. Static is NOT global. Static makes a variable a class variable as opposed to an instance variable. The following line is perfectly legal, but is not a global variable: private static var punch = ...; only the public modifier makes a variable global (its the default in js). Static has nothing to do with the variable's accessibility level.

@Peter G Wait, are you saying the public keyword alone is sufficient to make a var a global or are you saying public + static keywords make a global? Would you call the first of these two a global? public var punch = ...; public static var punch = ...; So far, I had been under the impression static was enough to define a global, but I'd have no problem to learn that public static is required. But only public...? now that would break my view of the world...? :p

What he said is correct - I should had read his comment more carefully. static var is the same as public static var (public is default in Unityscript), and defines a global scope variable. If you write private static var you create a variable with program life, but script scope. By the way, a static variable exists even if you don't ever instantiate the script where it's defined, and continue existing if you destroy that script (at least the public ones).

Complementing: public alone doesn't make a variable global - it should be public static, but static alone does the job because public is default.