How to find local player gameobject? c#
So I have a UI that pops up and the player selects from two teams. When a player clicks one of the buttons, it then runs a function on a script, which is then supposed to set some variables, then run another script, on the local player, which then instantiates the appropriate weapons. How would I find which player the local player is? Alternatively, how could I make a UI button OnClick() event do something on the player?
Answer by Willbl3pic · Feb 17, 2016 at 10:45 PM
Never mind, I'll just work around it by getting the player GameObject to do it all itself. I don't know why I didn't do that in the first place.
Answer by Salmjak · Feb 17, 2016 at 08:46 PM
Just use "`if(isLocalPlayer)`". Then the script will only run on the local player.
@Willbl3pic Then it depends on what you mean by "local player". Any client is in itself the local player.
You really have to give us more info. How does the script not run on the local player? Do you use a if(isServer) check in the script? Do you use [Command] or [ClientRpc]? What are the variables that are going to change? Why can't you use [Command] + [SyncVar]?
In any case I'm sure you can use the $$anonymous$$essage class.
By the local player I mean the GameObject which the player is controlling.
Salmjak has the right idea, you need to give us more information. What do you mean
it does not run on the player?
Answer by Integrational · Jan 11, 2018 at 02:34 PM
@Willbl3pic Use a for loop to go through every instance of an active GameObject of tag "Player" (GameObject.FindGameObjectsWithTag("Player")), and while in the for loop, get the network identity component on the GameObject of index i, and check if the isLocalPlayer variable on that component is true. If so, then the GameObject of index i is your local player character.
Your answer
Follow this Question
Related Questions
UNET Clients Laggy 0 Answers
How to decide an asynchronous multiplayer solution for a turn based mobile chess game? 0 Answers
Locally disabling GameObjects with network transform components attached. 1 Answer
UNET NetIDs for Scene Objects Mismatched Between Client and Server 1 Answer
Unity Networking Cannot Join Match - Room Disappears 1 Answer