Use GUI buttons to move a character.

Hi, i am new in unity and i was stuck for a few days with this problem. I need make a character jump when a gui button is getting pressed, but the problem is when i press the button nothing happend, ever if i just try to print something in the console nothing happens.

	public GameObject mo;
	public int force = 0;

	void Update (){

	}

	public void OnGUI (){
			
    if (GUI.Button (new Rect (-277, -208, 160, 30), "Button")) {			
    mo.GetComponent ().velocity = new   
    Vector2 (mo.GetComponent   ().velocity.x, force);
				
      Debug.Log ("press");
				
			
		}
}

}

I don´t know what its wrong, i drag the script in te canvas button and try in other objects but notting happens.

Sorry for the bad english and thanks for reading.

Well, first it would be useful to note that the OnGUI() function is somewhat dated. You’d be better off going into create->UI->button. It makes everything much more user friendly, easier to manipulate, and gives direct access to things like OnClick() events right in the inspector. I’m not entirely sure why your debug.log isn’t printing, but again, that issue would probably go away if you switched to the newer UI button method.

Also, the reason your character isn’t jumping is because your get component call, I believe isn’t being called correctly, but, either way, there is no component called “velocity” that can be attached to the game object. To get velocity for a character, generally you would attach a character controller component that then contains a property called velocity, and you would get component on the character controller and then access it’s velocity. Of course, if your “character” object is simply a cube or something that you want to apply a uniform force to, then you would use RigidBody2D.AddForce(), which will create a slightly different effect dependent on what you’re going for (note also that this isn’t a preferable method for jumping in most cases).

So, if I were you, now I don’t know how it fits into the context of this project, this is what I would do. I would start by adding a button to the UI under create->UI->button as a child of the canvas object. Then I would make a script called something like ButtonEvents to store button events for my canvas, and attach it to my canvas or some object where you know where it’ll be. In that script I would include a function like this to be called when the button is clicked:

public class ButtonEvents : MonoBehaviour
{
	public GameObject mo;
	public int jumpVelocity = 10;
	//I made that number up, you'll have to play around with jumpVelocity
	
	CharacterController charController;

	void Start()
	{
		charController = mo.GetComponent<CharacterController>();
	}

	public void jumpButton()
	{
		if (charController.IsGrounded)
		{
			charController.velocity.y += jumpVelocity;
		}
	}
}

Then, in the inspector window of my button that I added to the canvas, there is a box called OnClick(). Click the plus sign in the bottom right hand corner, then select the gameobject that the ButtonEventsScript is on, then select the function called JumpButton() from the dropdown list. Now the JumpButton() method will be called every time that button is clicked and the character will jump, assuming he’s not already jumping.

That’s just how I would do it at least, because OnGUI doesn’t really see much use anymore, and is generally considered less efficient on the computer to my knowledge. Bit of a lengthy response, but I hope this helps you fix your jumping issue.