I asked for help getting a random player system and they gave me the code below, but TaggedPlayer is a string. I need to be able to enable a script on the random player. How do I make it where I can use .GetComponent on the TaggedPlayer? Please help, I’ve been stuck on this part of my code for 1 whole month now…
public List<string> PlayerList = new List<string>();
void OnPhotonPlayerConnected(){
if (!PlayerList.Contains (PhotonNetwork.playerName)) {
PlayerList.Add (PhotonNetwork.playerName);
}
}
void TagRandomPlayer(){
int PlayerListRange = PlayerList.Count;
System.Random Rand = new System.Random ();
int RandomPick = Rand.Next (0, PlayerListRange);
string TaggedPlayer = PlayerList [RandomPick];
}
The GameObject that is controlled by each player and the actual PhotonPlayer are actually two separate objects, and the problem you have is that they’re not linked together in some way. What you could do is add a Dictionary so that it can keep track of the controlled GameObjects. It’s a little hard to tell what the best way to populate the dictionary without more detail, but what I often do is have the main script that drives the players’ GameObject “register” with the main NetworkManager script. So in the PlayerInput’s (or whatever the script may be) Start() method:
string myPlayerName = photonView.owner.name;
NetworkManager.RegisterPlayerObject(name, this.gameObject);
Then in NetworkManager (which seems to be the function of the script you posted above):
Dictionary<string, GameObject> PlayerDictionary = new Dictionary<string, GameObject>();
public static RegisterPlayerObject (string playerName, GameObject playerGameObject) {
NetworkManager nm = GameObject.Find("NetworkManager").GetComponent<NetworkManager>();
nm.PlayerDictionary.Add (playerName, playerGameObject);
}
Then when you want to get the GameObject for a particular player (or a component on that GameObject):
// Code from OP...
string TaggedPlayer = PlayerList [RandomPick];
// For example, get the PlayerInput component:
PlayerInput pi = PlayerDictionary[TaggedPlayer].GetComponent<PlayerInput>();
I hope that helps! You could also look into the TagObject property of PhotonPlayer (http://doc-api.exitgames.com/en/pun/current/pun/doc/class_photon_player.html#aaf54b32878a605d3e4d47f16ad106aa3), but I haven’t had much success with it personally.