0
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{
    public Vector2 jumpForce = new Vector2(0, 300);
    public GameObject Egg;
    public Transform eggpoint;

    // Use this for initialization

    void Start()
    {

    }


    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyUp("space") || Input.GetMouseButtonDown(0))
        {
            GetComponent<Rigidbody2D>().velocity = Vector2.zero;
            GetComponent<Rigidbody2D>().AddForce(jumpForce);
        }

            if(Input.GetKeyDown(KeyCode.Return))
            {
                Instantiate(Egg, eggpoint.position, eggpoint.rotation);

}

So as you guys can see in the separate box how I am instantiating my egg droping code. And I am achieving this by pressing the return or enter button on the keyboard, what I want to ask is how can I use a button to do it rather than the enter. So I have placed a UI button on my game screen but as I am absolute beginner I can't figure out how to connect the button and the function please guide me to the right tutorial.

Trisped
  • 5,705
  • 2
  • 45
  • 58
jack
  • 1
  • 1

2 Answers2

0

Jack,

I would highly recommend that you look through the Unity Tutorials as they will guide you through some of the basics of UI development.

See: Unity3D UI Tutorials

In regards to your question:

In Unity, if you click on your button gameobject, in your inspector you should see a button component attached to the gameobject. This button component will have a feature called "On Click" which you can use to trigger an action to occur.

The other way to go about it would be to reference the Button component via code and add an event listener that calls the appropriate function when an event occurs.

I have a short tutorial video that explains components that can be seen here: Video Link

Eissa
  • 700
  • 5
  • 17
0

I would this

 if (Input.GetKeyDown(KeyCode.Space))  || Input.GetMouseButtonDown(0))
            {
  GetComponent<Rigidbody2D>().velocity = Vector2.zero;
            GetComponent<Rigidbody2D>().AddForce(jumpForce);
            }
CubeCrafter360
  • 192
  • 1
  • 13