Problem with Button and Functions
Hey, I'm currently trying to program something like a turn based combat system. Recently, I added an Interface with Buttons with the actions a character can do. Right now I'm working on the walk button.
I wanted to use one general function that I give a boolean that changes its value to true when the function is used. But the problem is, that this doesnt work. If I write another function and use the specific boolean instead of a general one, I dont get the problem. But I dont think it looks that good, if I have 3+ times the same function with just another boolean variable.
public Button walkButton;
private PlayerAttributes pa;
// Use this for initialization
void Start () {
pa = GetComponent<PlayerAttributes>();
//walkButton.onClick.AddListener(SetMove);
walkButton.onClick.AddListener(delegate { SetPlayerAction(pa.canWalk); });
}
private void SetMove()
{
pa.canWalk = true;
}
private void SetPlayerAction(bool action)
{
action = true;
}
}
SetPlayerAction should be the function I use for every command the controllable character can do. So if I click the Button "Move", the character shall be able to move. But it doesnt work with "SetPlayerAction". Thats why I wrote the same function with a specific variable and it worked right away. But that would mean that I copypaste this function for each command. Can anyone tell me if it works the way I want it to or should I do it the way it works currently?
Thanks in advance
Your answer
Follow this Question
Related Questions
Toggle a bool on only one object at a time by tapping an object. 0 Answers
How to get access to bool from one script to another with touch input? 1 Answer
Buttons to make the ball move left and right 0 Answers
Functions on GUI elements disapearing after reloading the scene 1 Answer
Game crashes when function is triggered, 0 Answers