- Home /
HOW TO CHOOSE ONE OF THE PLAYERS AND TRANSFORM IT IN A PREFAB?
Hi ! I am a beginner on Unity and I have a problem... I am currently creating a game that uses UNet and, once the number of players connected in a room, starts a game. During the game there will be one of the 10 players who will be chosen randomly and will become a hunter. The others will have to go hide themselves.
So I don't know how to make sure that one of the players is chosen at random and turns into a different prefab when starting the game.
I do not know where to start so any help would be welcome !!
PS: Excuse me if there are misspellings I am French and I'm not strong at english
Answer by dargonknight · Mar 20, 2019 at 02:17 PM
@AXORTH there are a number of ways to deal with the this for beginners I would suggest that you pick a player at random from the server and activate a bool "IsHunter" (it could even be an other script or child gameobject) while turning off the initial settings of the "hunted" this would make it possible for you to make 1 prefab that contains all of your settings instead of trying to change the spawn etc... (even though your request is possible this is the easiest approach u can have).
SMALL FRENCH EXPLANATION:
il te faudrai un avoir les 2 object dans le meme prefab player et puis tu active le Hunter pour le joueur que tu choisi et tu desactive l autre du coups sa te simplifira le travail enorement.
Answer by AXORTH · Mar 20, 2019 at 02:29 PM
Thank you @dargonknight for making a French translation! Thanks to you I understood how to do Did you know how to select at random one of all connected players ? Thank you !
@AXORTH pour la prochaine fois utilise "Add Comment" pour faire un commentaire et ne pas ajouter une reponse. as for the random selection on the server you can go as simply as getting the list of the connected players and using the function Random.Range of unity.
public void selectRandomPlayer(List <Player> players)
{
//random index
int index = Random.Range.(0, players.Count);
//function where you turn ur player to a hunter but dont forget to use the proper networking functions this script is solely a selection script (preferably used on the server and RPCing the answer to other players etc...)
players[index].setHunter();
}
French explanation: le bute ici est de simplement faire une liste de tes joueur et utiliser la fonction proposer de unity pour avoir un index aux hasard que tu utilisera pour faire ton choix
@dargonknight Thanks for all ! The only problem is when I type the code, the word "Player" is wrong... Yet my player is named Player Photo here.
@AXORTH my code was an example the Player is the class i m using you should use your own class