- Home /
How do I find the player GameObject of the local player?
Hello! I've been playing around with network code for a while now to try and make a multiplayer RTS. So far I'm only testing concepts, to find a structure that works before committing to making a game.
I've realized that I want the units to keep track of an empty player GameObject that contains a script with player information, like player number, colour etc. This player GameObject spawns when hosting or joining a game because I'm using the basic NetworkManager at the moment.
I've been doing this in order to find the local player in the UnitTest class:
public void FindLocalPlayer() {
foreach(GameObject cur in GameObject.FindGameObjectsWithTag("Player")) {
Debug.Log (cur);
Debug.Log ("Iterating");
if(cur.GetComponent<RTSPlayerTest>().isLocalPlayer == true) {
Debug.Log ("Found local player");
localPlayer = cur.GetComponent<RTSPlayerTest> ();
}
}
if (localPlayer == null) {
Debug.Log ("Couldn't find local player");
}
}
but I always get the "couldn't find local player" message, which baffles me because this code in the RTSPlayerTest class, that's supposed to assign player numbers functions in the same way but works perfectly:
int i = 1;
foreach(GameObject cur in GameObject.FindGameObjectsWithTag("Player"))
{
if (i <= cur.GetComponent<RTSPlayerTest> ().number) {
i++;
} else {
number = i;
Debug.Log ("Player number assigned: " + number);
break;
}
}
If this has already been asked I apologize, but I couldn't find anything similar on google.
It seems like there was nothing wrong with my code. After checking the inspector, I see that my player prefabs don't spawn, which is weird because I'm using the stock Network$$anonymous$$anager. Should I delete the question since it's obsolete, or will my code snippets posted here be of any help to someone else wanting to find th local player?
Answer by FatmaGurel · Dec 01, 2017 at 10:57 PM
I don't know if this is the right way to do this, but what I do is in Player Controller Script I do the following:
public override void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();
gameObject.name = "Local";
}
And in my Game Manager I use GameObject.Find("Local") to do things thereon.
This makes referencing the local player so much easier! Thanks a bunch for this idea! :)
This solves not only the original problem but identifying a player over a network as well. Good work!
Answer by UDM1983 · Jan 20, 2017 at 10:53 AM
If you assigned a NetworkIdentity component on your player prefab, you can try:
if (cur.GetComponent<NetworkIdentity>().isLocalPlayer)
{
//do whatever you wish here
}
Did not test it myself though.
But what if you need to know the difference between four players?
So you save that
player 1 is called Bob
player 2 is called Annie
player 3 is called Sylvester
player 4 is called Arnold
To save that your four players have different names, you'd need to give them each a different ID, not just true/false LocalPlayer. I'm guessing there is a Network function for this
Your answer
Follow this Question
Related Questions
how do i network sync scaling of character? 1 Answer
Child of a player not syncing position with network? 0 Answers
uNet horrible lagg (even on local servers) 0 Answers
Chat system is not working 1 Answer
uNet Offline and Online Scenes the Same 2 Answers