i have some ui buttons and 1 - 4 players. the players can all move and attack and whatnot independently but if any player clicks a ui button it applies to player 1 always. in the editor during gameplay i can manually drag in a different player to the buttons target and then that player can use the buttons properly. obviously in a real game you cant access the editor so how do i detect which player clicked a button and then reassign the buttons target to be that player.
i.e.
ButtonScript // attached to canvas
{
public PlayerScript player; // attached in editor
public void DoButton(Gameobject Object )
{
player = player who clicked // this is the line/lines i cant figure out
player.ReciveObject(object)
}
}
- Try to make sure that only one script handling local input is in the scene, usually this is the local player and you have some way in some code to determine if it is the local player or not and destroy the components on other replicated players in the scene.
- Know that local input is only coming from a local player.
Your question is only relevant if it’s a couch game where multiple local player can give input, in this case maybe you have 4 copies of the input script and when the scripts are created in the scene you use the exact same script but have a property that is an integer that you set on each script to be 1,2,3,4 and in your code you check the number and read input from that controller. There will be some way in code to read input from different controllers.
Add a Public Int PlayerNumber; to your script, then in the editor you can have 4 copies and set that integer to be 1,2,3,4. In your code you will either use a switch() or if() statement to check that integer and read from one of the controllers. If you aren’t doing a local couch multiplayer game and it is a true online multiplayer game than what I mentioned in the beginning is true: Try to destroy any other copies and know that the input will always be from the local player. Remote players are irrelevant for input.
If you are doing couch multiplayer with the New Unity Input System, you can look at the Simple Multiplayer sample included in the Input System Package that shows how to setup local couch multiplayer:
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Components.html#playerinputmanager-component
You can also try this video, which shows adding the Player Inputs and PlayerInputManager - but you should review the documentation and see if the official Simple Multiplayer sample does what you need first:
thank you, ill try to apply this. it is for a couch coop game using the new input system and each player does have an index number.