- Home /
button to select what player shoots
The Player shoots automatically..what it shoots depends upon which button is clicked button 1 clicked then projectile 1 is shot...button 2 clicked then projectile 2 is shot and so on I have buttons instantiated from a prefab depending upon what projectiles are available to shoot.
i am confused about a decoupling solution to where the buttons when clicked should assign projectileToshoot in player class...
so was thinking about events but then playerShoot class would need to subscribe to all the buttons to see which one is pressed to assign the projectileToshoot.. how should i go about this ? @Bunny83
you could simply add a listener when the button is instantiated?
GameObject buttonInstance = Instantiate(yourButtonPrefab);
buttonInstance.onClick.AddListener(() => FireBullet(bulletType));
FireBullet(string bulletType)
{
Debug.Log("firebullet");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613004152im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to have a callback when a toggle is clicked in a toggle group? 3 Answers
Is reference better than UnityEvent ? 1 Answer
Is there a way to know when mouse is 'hovering' over a Toggle? 2 Answers
Is there anyway to stop event propagation 1 Answer
Help I am getting an error CS0079 in my code and I don't know why. 1 Answer