create object and add increment number to it's name

is that possible?

var nick:String = “enter name”;

class Player{
  var nick:String;
  var score:int;
  function Player(n:String){
    this.nick = n;
    this.score = 0;
  }
}

function OnGUI (){
	nick = GUI.TextField(new Rect(10,10,150,20),nick);
	if (GUI.Button (Rect(10,50,100,30),"add player")){
		var i:int = 1;
		var Player *= new Player (nick); // should be the same as var Player1*
  •  i++; //number increments with each player added    		*
    
  • }*

  • if (GUI.Button (Rect(130,50,100,30), “Start”)) {*

  •  Application.LoadLevel(2);*
    
  • }*
    }
    as the result i need to get Player1, Player2… Playern
    also, how do i get these objects accessible in another scrip? static won’t help here, will it?

I think you’re a bit a confused between the type string, which is a value you can affect to a variable, and the name of that variable which is the point to your compiler what is going on.

Anyway, you can’t write that as Player is a type, not the name of a variable. Furthermore, even with a variable, you can’t declare arrays like that.

var Player *= new Player (nick + i);*

So, first, declare a variable playerCount (makes more sens than i) and an array of Player, outside of the scope of a function. With C#, it would have been more obvious what scope you’re in when outside a function, but nevermind.
var playerCount : int = 0;
var players : Player[]; // That’s how you declare an array in JS right ?
Then, you need to initialize that array in your Start or Awake function.
function Start()
{
// I’m not used to JS, not sure that’s the right way
players = new Player[10]; // I set player max to 10, you can change it. There is other more convenient ways, but no need to complicate things.
}
Finally, when you press the button, create a new player and increment the counter :
players[ playerCount++ ] = new Player(nick); // I didn’t check for out of range, be careful !

The problem here is that i is local to the if statement. If you declare i (maybe give it a better name, too) outside of OnGui() , and add it to nick, it should work. I didn’t test this but in theory:

var i:int = 1;

function OnGUI (){
    nick = GUI.TextField(new Rect(10,10,150,20),nick);
    if (GUI.Button (Rect(10,50,100,30),"add player")){
       var Player *= new Player (nick + i); // should be the same as var Player1*

i++; //number increments with each player added
}

Oh, and to answer the second part of your question, how to access and whether to use static: