- Home /
OnClick Event getting called on another event
Hello So i have a script with a onClick Event:
public class SkillTree : MonoBehaviour
{
public Button healingButton;
void Awake()
{
healingButton.onClick.AddListener(HealingClick);
}
void HealingClick()
{
Debug.Log("Click!!");
}
And on another script i have a space event:
public class Player : MonoBehaviour
{
void Update()
{
if (Input.GetKeyUp(KeyCode.Space))
{
Debug.Log("Space!!");
}
}
}
If i press space after playing the game im getting the debug log of the space press only, but after i click the button event and start pressing space again for a weird reason i'm getting the 2 debugs logs on my console, both methods are getting called on only space press i'm seriously at lost of why is this happening using unity 2019.3.0f6 if that helps thanks in advance
Answer by Elango · Jul 25, 2020 at 07:18 AM
This is expected behavior. Space is one of the default Submit key. If you have selected Button element and pressed the key assigned to Submit, the OnClick event will be triggered. You can change the settings in Project Settings -> Input Manager -> Submit.
Answer by random61 · Jul 25, 2020 at 03:42 PM
So it looks like i had the bad luck of using space that also use the navigation hotkeys on the buttons. But thanks to you i solved it by changing all my buttons from navigation: automatic - to none. I scratched my head for a good 2 hours before asking here, but i learned a really important thing on unity Thank you very much for the help Elango