Diana
Diana

Reputation: 405

Unity 2D - Why does button not work?

OP figured it out, was missing EventSystem object

I made a running game by Unity. But I faced a big problem. I made one button to jump and add onclick event, however, It is not working. My Runner doesn't jump on ground. Please Help me!

enter image description here

Button and Button Inspector.

enter image description here

Runner Inspector.

enter image description here

OnClick Inspector.

And This is a Runner's C# Script :

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

    public class CsRunner : MonoBehaviour
    {
        public Vector2 jumpVelocity;
        public Text _coin;
        int coinCount = 0, heartCount, healthCount = 0, grimReaperCount = 0, coupleCount = 0;
        bool isJump;
        public Button _jump;

        bool collision_box;

        // Use this for initialization
        void Start()
        {
            _jump.onClick.AddListener(Jump);
            heartCount = 3;
        }

        // Update is called once per frame
        void Update()
        {
            _coin.text = ""+coinCount;
        }

        void OnTriggerEnter2D(Collider2D coll)
        {
            if (coll.transform.tag == "Enemy")
            {
                if (heartCount > 1)
                    Destroy(GameObject.Find("heart ("+(heartCount-1)+")"));

                else if (heartCount == 1)
                    Destroy(GameObject.Find("heart"));

                heartCount--;
            }

            if (coll.transform.tag == "HealthBuff")
            {
                Destroy(coll.gameObject);
                healthCount++;
            }

            if (coll.transform.tag == "GrimReaperBuff")
            {
                Destroy(coll.gameObject);
                grimReaperCount++;
            }

            if (coll.transform.tag == "CoupleBuff")
            {
                Destroy(coll.gameObject);
                coupleCount++;
            }

            if (coll.transform.tag == "FieldCoin")
            {
                Destroy(coll.gameObject);
                coinCount++;
            }
        }


        void OnCollisionEnter2D(Collision2D coll)
        {
            if (coll.transform.tag == "Ground")
            {
                GetComponent<Animator>().enabled = true;
                collision_box = true;
                isJump = false;
            }
        }

        void OnCollisionStay2D(Collision2D coll)
        {
            if (coll.transform.tag == "Ground")
            {
                GetComponent<Animator>().enabled = true;
                collision_box = true;
                isJump = false;
            }
        }

        void OnCollisionExit2D(Collision2D coll)
        {
            if (coll.transform.tag == "Ground")
            {
                GetComponent<Animator>().enabled = false;
                collision_box = false;
                isJump = true;
            }
        }

        public void Jump()
        {
            Debug.Log("JUMP!!");

            if (isJump)
            {
                isJump = false;
                transform.GetComponent<Rigidbody2D>().AddForce(jumpVelocity / 2, ForceMode2D.Impulse);
            }

            if (collision_box)
            {
                isJump = true;
                transform.GetComponent<Rigidbody2D>().AddForce(jumpVelocity, ForceMode2D.Impulse);
            }

            else
            {
                GetComponent<Animator>().SetTrigger("Run");
            }
        }
    }

Thanks!

Upvotes: 0

Views: 1953

Answers (1)

Diana
Diana

Reputation: 405

I solve the problem!! Canvas Object hasn't EventSystem!! Thank you everyone:)

Upvotes: 1

Related Questions