- Home /
How make a character jump using a UI Button and the void OnGUI?
Hi, i am new in unity and i am stuck with this little problem. I need make a character jump when the player is pressing a button in the screen.
To make that i create a button (GameObject => UI => Button) and put that script inside it.
public GameObject monster;
public int force = 0;
public void OnGUI (){
if (GUI.RepeatButton (new Rect (-277, -208, 160, 30), "Button")) {
monster.GetComponent<Rigidbody2D> ().velocity = new Vector2 (monster.GetComponent<Rigidbody2D> ().velocity.x, force);
Debug.Log ("Jump");
}
}
}
But when i press de Button nothing happens and i can´t find whats its wrong, even if i just try print something the void OnGUI never get executed.
I try to solved the jump problem using the OnClick and trigger events in the UI button components but i don't find a event that works like "GUI.RepeatButton". If somebody know what i do it wrong or have a example of how use the void OnGUI pressing a button will be very helpful.
Thanks for reading.
Your answer
Follow this Question
Related Questions
Canvas-prefab elements are not interactable 2 Answers
Best use of GetButton 4 Answers
UI Buttons dont react on click 4 Answers
Help much appreciated with buttons and text fields please 0 Answers
How can I make my car turn? 1 Answer