void ConfigButtonListeners()
{
buttonKeyTypes[Profile.keyType.LEFT] = buttonKeyLeft;
buttonKeyTypes[Profile.keyType.RIGHT] = buttonKeyRight;
buttonKeyTypes[Profile.keyType.JUMP] = buttonKeyJump;
buttonKeyTypes[Profile.keyType.SHOT] = buttonKeyShot;
buttonKeyTypes[Profile.keyType.PICKUP] = buttonKeyPickup;
foreach(KeyValuePair<Profile.keyType,GameObject> pair in buttonKeyTypes) {
Button button = pair.Value.GetComponent<Button>();
Debug.Log(pair.Key); //gives right value
button.onClick.AddListener(
delegate {
isConfiguring = true;
Debug.Log(pair.Key); //gives wrongvalue
configButtonPointer = pair.Value;
});
}
}
1.) Inserting all public GameObjects in dictionary (works correctly, I checked the values)
2.) For each dictionary value(GameObject) I’m getting it’s Button component
3.) I’m defining what will happen onClick on every of those buttons over delegates (Problem is here somewhere)
4.) When I click on one of those buttons 'configButtonPointer ’ will refrence to that object that was clicked
The problem is that ‘configButtonPointer’ is always refrencing last value(GameObject) in dictionary which is buttonKeyPickup, instead of being refrence to a GameObject that holds button that was last pressed.
I hope you understand what I was trying to say and thanks in advance.