Looking for advice about how to streamline my code (avoid repetition)
I've been taking some online courses about how to make video games in Unity (with C#), and I just finished my first game that I did all on my own. It all works perfectly fine, but there's a lot of repetition in the code that seems like it probably not the best way to do it. Like, there are six attack buttons (three for each player) that all do basically the same thing, but I had to write separate lines for each of them, because I couldn't figure out how to do a generic version of it. The way it works (this was just a really simple game to practice the mechanics) is that the attack value of the button you pressed goes down after the attack, and the attack values of your other two buttons go up. But I couldn't figure out any generic way to code, "the button you pressed does one thing, the other two buttons of the same player do something else, and the other player's buttons do nothing." So, for instance, the code for pressing attack button A looks like this...
public void activateAttackA(){
twohp = twohp - attackA;
twohptext.text = twohp.ToString();
attackA = attackA - Random.Range(1,4);
attackB = attackB + 1;
attackC = attackC + 1;
playerTurn = 2;
StatusDisplay();
}
...And then, the code for attackB is the exact same eight lines of code repeated, just with the relevant letters changed. There's probably a more smooth and streamlined way to do that, right?
Your answer
![](https://koobas.hobune.stream/wayback/20220612152357im_/https://answers.unity.com/themes/thub/images/avi.jpg)