I have a problem, when I select the first car in the menu of my game everything is normal, because the canvas has the references of the buttons that are in the “Car Player”, but when I select the other car in the menu “Car Player ( 1)”, the references continue to “Car Player” but should change to “Car Player (1)”. So I want to know if there is any way to change these references through the script, is this possible?. I haven’t found a solution to my problem so I’m here summoning the smartest ones.
Don’t have the button know anything about the car.
The button should only signal “I clicked the FOO button!”
Then the code can track what car you want to perform FOO upon.
Otherwise, if you insist on adding the car to the button, you DO NOT add it in the editor, but rather using the .AddListener() API on the onClick method of the button. Enclosed is an example of that kind of dynamic listener across several buttons, but you could add / remove the listener on a single button. I still recommend going with the first approach however, where the button knows nothing about the car.
Yeah, normally you’d have an EventDispatcher script that gets all of the GUI events and relays them. That way you can change which code runs for which button at any time, or even have multiple things run, or none.
An alternative is using ScriptableObjectVariables as the target that also send out value-changed (ScriptableObject)Events.
My Datasacks package uses this sort of paradigm. You just put a standard DSUserIntentButton script on a given Button and then it will signal the name of that GameObject when the button is pressed.
Anywhere in code can listen to that signal and decide what to do when they see the button was pressed.
By clustering scripts along with the prefabs of popup windows or panels or scenes or whatever, you can carefully control which code is even present and running when a given button is shown as part of a UI.
Anyway, that’s how much I hate dragging specific linkages of my game logic into buttons. I would rather that game logic just sit over in code listening for UI events, all of which are super-generic button intent replicators as above.
Trabalhar com botões é uma dor, mas realmente você está certo sobre o lado ruim da lógica do jogo estar nos botões, estarei testando seus scripts e ver o que posso fazer no meu projeto, agradeço muito as dicas e gostei do seu jogos btw