Hi ! It’s been a while now that I am on the same problem, after trying everything and looking in all the corners, I ask here: How to detect which prefab player to press on a UI button? Say otherwise and targeting more precisely my problem: How to get a value of a script attached to one of my players from the submit of a ui button?
I describe a bit more: I have a local multiplayer game (using gamepads, like a classic local multiplayer game like, Boomerang Fu, Stick Fight, or even Street Fighter!), I have indexes assigned to each new player , to see who is the P1, the P2 etc … I have several buttons that represent “characters”, which is information that I need to put in Player scripts.
So far nothing really crazy, I know how to do it and besides I have already done it for a player. However, when it comes to removing a value that can change from one script to another, knowing that P2 can press one character and P1 another, it gets more complex.
Example of a script on the player (and necessarily on the player otherwise the Listener does not take any value specific to the player):
// Lignee = Character like in an classic RPG Game
public void BtnTrigger()
{
GameObject[] btnLignee = GameObject.FindGameObjectsWithTag("ButtonListLignee");
foreach (GameObject btn in btnLignee)
{
btn.GetComponent<Button>().onClick.RemoveListener(() => ButtonChoice(btn.GetComponent<Button>(), Player));
btn.GetComponent<Button>().onClick.AddListener(() => ButtonChoice(btn.GetComponent<Button>(), Player));
}// end
MainMenu.ChoiceMenuActive = false;
}
public void ButtonChoice(Button btn, DataPlayer.Lignee p)
{
foreach (var l in Record.Lignee)
{
int actualPlayerID = l.ActualPlayerID;
string name = l.name;
var desc = l.desc;
if (l.id != btn.GetComponent<BtnListIndex>().indexBtn && actualPlayerID == p.ActualPlayerID) // reset
{
actualPlayerID = 0;
l.ActualPlayerID = actualPlayerID;
}
else if (l.id == btn.GetComponent<BtnListIndex>().indexBtn && actualPlayerID == 0)
{
Debug.Log("Player: " + p.ActualPlayerID + " choose : " + name);
actualPlayerID = p.ActualPlayerID;
l.ActualPlayerID = actualPlayerID;
p.name = name;
p.desc = desc;
}
else if (actualPlayerID != 0 && actualPlayerID != p.ActualPlayerID)
{
Debug.Log("Already chosen");
}
}
}
It works for 1 player only, but all the players are P1, because the Listener is only started on the script of player 1, otherwise it would make too many Listener …
I started looking at something else like this for example: 21/20/oxw6.png - Visionneuse Zupimages
Because from here, i can take the Index of each player, but impossible to get on which button ui the submit is launched … I obviously tried to call the Listener from an object other than the P1, without success knowing that it never managed to reach the values intrinsic to the scripts of each player…
Anyway, i need help, thanks !
I forgot to say that i am using the new input system, with a MultiplayerEventSystem present in a childobject under the player, i tried to get the information from the eventSystem to detect which player pressed what, without success.
Sorry if it’s not well explained, strangely i rarely saw anyone explain my problem elsewhere, otherwise no answer or not quite the same things.
I’ve already asked the question here, without success : https://forum.unity.com/threads/detect-which-prefab-triggers-the-button-ui.1111288/