OP弄明白了,缺少EventSystem
对象
我用Unity制作了一款跑步游戏。但我遇到了一个大问题。我做了一个按钮来跳转并添加onclick事件,但是,它不起作用。我的跑者不会跳到地上。请帮帮我!
按钮和按钮检查器。
Runner Inspector。
OnClick Inspector。
这是Runner的C#脚本:
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");
}
}
}
谢谢!
答案 0 :(得分:1)
我解决了这个问题!! Canvas对象没有EventSystem !!谢谢大家:)