Creating new players

Updated on January 18, 2017 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
11 on January 17, 2017

I am making a simple game and I am stuck.

I have a part of the game where the player can choose to either create a new player or choose one of the existing ones. Each player should contain a name, number of losses, wins and a color (I am using a color picker).

I tried making a separate class called Player with those values. But the problem is that when I create a new Player I need a variable name for that player. How would I do this?

  • Liked by
  • Leon Laci
Reply
1 on January 18, 2017

Hint: you can use indtantiate() to create a new player

Devoted
on January 18, 2017

I don’t mean it as a game object, I mean as an object in OOP programming which has it’s own properties.

Show more replies
  • Liked by
Reply
Cancel
8 on January 18, 2017

You could use a Dictionary that has an integer for a key and a Player for the value

Devoted
on January 18, 2017

I never used dictionaries and don’t know how they work, will check it out.

Devoted
on January 18, 2017

I looked at dictionaries a bit, so I can every time I create a player use a variable to increment every time.

int i = 0;
Dictionary.Add("Player" + i, new Player());
Guru
on January 18, 2017

Yes that’s how it’d work

Devoted
on January 18, 2017

And I just use accessors to make a new Player()

Devoted
on January 18, 2017

So I should make the i variable static? But what happens when I exit the game, the i will reset, PlayerPrefs?

Guru
on January 18, 2017

That’s your to decide but you do indeed need to save the dictionary in some way

Devoted
on January 18, 2017

Wow, totally forgot that I need to save the dictionary too, any tips?

Guru
on January 18, 2017

There are multiple possibilities for saving but one that I think would work best for this is Protobuf although I’ve never used it myself

Show more replies
  • Liked by
Reply
Cancel