Create a function that spawn player (wich datas are stored in an array) randomly

I’m trying to make a function that spawn player stored in an array randomly but I’m having this problem": i don’t know how to take each value ( for ex. each firstname, lastname) of each user. The array is like this:

[{“firstname”:“daniele”,“lastname”:“daveri”,“username”:“dany”,“gender”:“male”},{“firstname”:“admin”,“lastname”:“admin”,“username”:“admin”,“gender”:“male”},{“firstname”:“prova3”,“lastname”:“prova3”,“username”:“prova3”,“gender”:“male”},{“firstname”:“pp”,“lastname”:“pp”,“username”:“pp”,“gender”:“male”}]

So for example for the player : “daniele” the system spawn a male player prefab and so for all player stored in the array.

Thanks!

You can do random spawning many ways however having learned my lesson from doing it all programmatically, I’d suggest doing it only partially.

In your scene you could make a game object for each valid spawn location and tag it as “spawnLocation”. Then using GameObject.FindGameObjectsWithTag(“spawnLocation”) you can an array of valid locations. Grab a random index from this array and use the object’s transform.position.

This allows for more control of what’s a valid location and avoids issues where the player could spawn in the terrain for instance.

If you’re using the first name to look up the rest of the information, you might like to use a Map. Maps are data structures that associate one piece of data (called a Key) with another (called a Value). So you would have a map like this:

PlayerData daniele = PlayerDataMap.get("Daniele");
//More code here

I know this isn’t a complete answer for you but hopefully it might help! Here’s some info on Dictionaries in C#, which have this feature: C# Dictionary Examples - Dot Net Perls

ok thanks ! however i don’t know how to take each values for each player in the array.
thanks!

You can do this by inserting an array into an array, here’s a small example of how the idea works (if i understood you correctly);

var mainarray : new Array();

mainarray.Add("firstname","daniele","lastname","daveri","username","dany","gender","male");

var playertospawn = mainarray[0]; /// gets first entry of the mainarray //

var playername = playertospawn[1]; /// gets 2th entry which is daniele //