Calling a JavaScript class constructor

according to CodeAcademy this is how i’m supposed to call a class constructor to create a new object.

function Player(name) {
  this.name = name;
  this.score = 0;      
};

function OnGUI (){
	if (GUILayout.Button ("add player")){
		var Player1 = new Player ("john");
	}
}

however “var Player1 = new Player ("john");” results in “BCE0125: Invalid declaration type ‘void’.”

Can’t find any related info anywhere :frowning:

I doubt CodeAcademy teaches UnityScript. This is Javascript you are using there. Us and Js are not the same.

You need:

class ClassName{
  var name:string;
  var score:int;
  function ClassName(n:string){
    this.name = n;
    this.score = 0; // Note it is 0 by default
  }
}