How do I use Input.GetButton, instead of OnMouse()
I realize this question may be "silly" but I'm new to C#. I'm using this script example i found for a keypad:
void Update () { Debug.Log (playerCode);
if (totalDigits == 4)
{
if(playerCode==correctCode)
{
playerCode = null;
theDoor.GetComponent<Animation> ().Play ();
theDoor.GetComponent<AudioSource> ().Play ();
Debug.Log ("Correct");
}
else
{
playerCode="";
totalDigits = 0;
Debug.Log ("Wrong");
}
}
}
void OnMouseUp()
{
GameObject.FindWithTag ("KeypadButton");
playerCode += gameObject.name;
totalDigits += 1;
keySound.GetComponent<PlaysoundScript> ().Clicky ();
}
This script works fine in the editor when using the mouse, But I want the "OnMouseUp Click" to be done with a gamepad button in the Input manager
I tried moving the OnMouseUp() function inside Update, adding Raycast etc. What am I missing? Any help would be appreciated, Thanks.
Answer by JJMGsoftworks · Jan 24, 2017 at 04:07 PM
I don't know how to do exactly what you need but i have an alternative. I always use the key up bool to check if a key is up.
Input.GetButtonUp()
simply fill in the key/button you are checking and if it is true just call a OnMouseup . You may need to make a new method name if doing this
if(Input.GetButtonUp(yourbutton))
OnMouseUp();
I hope this answer is what your looking for. It should at least get you going if your still stuck
This answer was very helpful, Thank you!
I called "On$$anonymous$$ouseUp()" from inside "Update()".
Ex. void Update() { If(GetButtonDown("Ok")) { On$$anonymous$$ouseUp(); }
When i click the keypad keys with the mouse... it still works! (prints individual key names ex. 1, 5, Cancel, 8, etc)
But, when i use the "Ok Button", for some reason, the console prints out a whole bunch of random number combinations... Ex. 157,9,Cancel659,8693,
Any idea why?
Thanks!
This is just a strong guess but try making a method exactly like on mouse up but name it something different. Then in on mouse up you could call that method and also call it if GetButtonDown(ok). This way it will be exactly as it is now but without the error.
I am guessing this will work based on experience with $$anonymous$$onogame so it may not help in unity. I am assu$$anonymous$$g that the On$$anonymous$$ouseUp cannot be evoked in code. Please get back to me if it works I can also explain further.