- Home /
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[i] = 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?
as fot the last question, appearantly public class should do the trick. right?
loading a new scene won't discard objects created in previous one?
sorry, strated learning javascript just a bit more than a month ago. been in unity for 1 week - so be patient with me :)
Answer by FakeBerenger · Nov 12, 2012 at 11:25 PM
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[i] = 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 !
correct on all counts :)
(WELL, your var changed from "playerCount" to "playerCounter", but that's beside the point..)
also, I'm going to give you the benefit of the doubt on the ORDER OF PRECEDENC$$anonymous$$.
Sorry, I used Player[i] for ilustration purposes only. didn't know how to explain. Probably should have used "Player"+i.ToString() ins$$anonymous$$d.
Anyway, there are many ways to skin a cat and $$anonymous$$e also solved some problems i knew were ahead of me.
Answer by JamieFristrom · Nov 12, 2012 at 09:24 PM
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[i] = new Player (nick + i); // should be the same as var Player1
i++; //number increments with each player added
}
I see your point - "i" will be restarted with every frame.
But still that doesn't solve the main problem. Nick is an argument that is passed to a function set one of parameters within the object. following your solution will result in
var Player = { nick : John1, score : 0, }
but i need to add 1 to 'Player' not 'John'
I remeber using something like var window("Player"+i) = new Player (nick);
but that doesn't work in unityScript
Ah, yes. Unity javascript is different from web javascript and takes some getting used to - one of the reasons I prefer C#. You want to be able to, after you've written the above code, access players with window.Player0 window.Player1 etc - but Unity javascript doesn't let you do that. I'd argue you don't really want to do that, anyhow - it's too easy to introduce bugs (whoops, misspelled the name or changed the case). If you do what FakeBerenger suggests you'll find you can easily iterate through the entire array of players ins$$anonymous$$d of having to copy-and-paste lines of code.
last night I came across name+="Remote" which changes the name of an object to indicate whether the game object was created by you or remote player (this is for other project, this one is a hotseat).
it was 1:20am, so i didn't check it out, but i dont see any reason why var Player = new Player (nick); Player.name+=i.ToString(); wouldn't do exactly what i need.
P.S.: originally, my code had variable name ins$$anonymous$$d of nick, but it called my player 'main camera' by default, so i changed it... i should have guessed it sooner
Ok, wrong again :( It appears, that name returns the name of physical object that script is attachet to.
Does everything in Unity has to be attached to a physical (including empty) object in 3d space?
This is far from original idea and a question, but i can create a GUI object for every player created which will show the name and score parameters.
Thinking out loud here
IT IS ALIVE!!! $$anonymous$$UAHAHAHAHAHAAAA!!!!
Also figured out this piece of code in the process: guiText.text = gameObject.transform.parent.GetComponent("PlayerAttributes").score.ToString();
$$anonymous$$ust be no biggie for most of you, but it makes me fell like next most brilliant thing since sliced bread!
Answer by JamieFristrom · Nov 13, 2012 at 12:36 AM
Oh, and to answer the second part of your question, how to access and whether to use static: