- Home /
Other
How do I use Get Component in this code
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];
}
There are actually two issues in your question, the first one, you are asking about a topic that has many threads already, some of them including detailed explanation.
http://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent
Second, you start your question with "they gave me the code below".
Those two indicates that you need to spend more time learning. Unity has a large section for beginner tutorials, you should use it:
http://unity3d.com/learn/tutorials/modules/beginner/scripting
Answer by zak-reynolds · Nov 28, 2014 at 06:16 PM
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.
Where did you get PlayerInput from? I'm getting errors on public static RegisterPlayerObject and Network$$anonymous$$anager.RegisterPlayerObject(name, this.gameObject);
EDIT: Fixed the errors for them... Put a void after static.
How do I activate the code? I'm using this to enable some code on a character and I can't seem to activate it. When I try to it says Assets/The$$anonymous$$urderer/Scripts/Network$$anonymous$$anager.cs(33,14): error CS1624: The body of Network$$anonymous$$anager.OnNewRound()' cannot be an iterator block because
void' is not an iterator interface type
Ah sorry, it wasn't meant to really be copy/pas$$anonymous$$ble and more a demonstration of the idea, and yeah the missing void was a typo on my part. "PlayerInput" is just the class name I use on my own player GameObjects, it should be changed to what you use (the analogous class in the $$anonymous$$arco Polo tutorial is called "NetworkCharacter"). Basically, the idea is that when each player's object is instantiated in your scene, it registers itself with your Network$$anonymous$$anager so you can use it through the dictionary.
As far as the error CS1624 that you're getting now, it doesn't seem to be related to the code above; are you using yield in that method? I found this forum post that might help you out with that error.
Also as a side note that I just noticed looking at it again, when you use OnPhotonPlayerConnected, I'm fairly certain it's only called for the client it's running on and each client that connects afterwards, but not any that are already in the game. So if you have 4 players (A, B, C, and D who connect in that order): A will have it called for all players; B will have it called for B, C, and D; C will have it called for C and D; and D will only have it called for itself. That's why I like to use the "registering" method above, and you could add
nm.PlayerList.Add(playerName);
to the RegisterPlayerObject method to avoid that problem with your list as well.
This is probably obvious, but I'm new to program$$anonymous$$g and the there's a problem with it that I can't figure out. Right now I'm just trying to make sure it works by using a yield and there's no errors, but it's not enabling my code. Here's some code from network manager that I'm trying to use to select a random person. What's wrong with this?
public List<string> PlayerList = new List<string>();
public bool offline$$anonymous$$ode = false;
Dictionary<string, GameObject> PlayerDictionary = new Dictionary<string, GameObject>();
public static void RegisterPlayerObject (string playerName, GameObject playerGameObject) {
Network$$anonymous$$anager nm = GameObject.Find("$$anonymous$$anager").GetComponent<Network$$anonymous$$anager>();
nm.PlayerDictionary.Add (playerName, playerGameObject);
nm.PlayerList.Add(playerName);
}
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];
$$anonymous$$nife knife = PlayerDictionary [TaggedPlayer].GetComponent<$$anonymous$$nife>();
(($$anonymous$$onoBehaviour)knife.GetComponent("$$anonymous$$nife")).enabled = true;
}
IEnumerator OnNewRound(){
yield return new WaitForSeconds(10);
TagRandomPlayer ();
Debug.Log ("Hello");
}
// Use this for initialization
void Start () {
spawnSpots = GameObject.FindObjectsOfType<SpawnSpot>();
Connect ();
OnNewRound ();
}
Follow this Question
Related Questions
Using a String to populate GetComponent 1 Answer
Using Random.Range in an array only gives a random output once 2 Answers
Is it possible: "getComponent<_array_element_$>"? 1 Answer
GetComponent(string) with unknown ScriptName 2 Answers
Random String from list of string without repeated letters 2 Answers