- Home /
all ui buttons applying to player 1, how do i make them apply to the player that clicked
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)
}
}
Answer by GetLitGames · Jun 13, 2021 at 12:24 PM
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: https://www.youtube.com/watch?v=iFrO4bqmOPU
hey thanks this helped, i got it to work messily, I set up an array in the button script and got it so the player sends its player number to the button script. the only thing stopping it working cleanly is ,using the new input system, i cant figure out how to do a Get button up on a script meant for a ui element. i can do for the actual player but when i add a player input component to a ui element, that element stops showing up on the canvas
OK now that I know what you are using, I added details about what to use. You should look at the Simple Multiplayer sample and see if you can modify the prefabs or code from that to do what you need since it's an official example.
Answer by Snowhouse · Jun 13, 2021 at 08:12 PM
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.
Your answer
Follow this Question
Related Questions
Character isn't moving in multiplayer with buttons. 0 Answers
Multiplayer UI scaling 0 Answers
Unity 4.6 UI - multiple joysticks 0 Answers
How to prevent touch to propagate through new UI Button? 1 Answer