PL,我正在Unity和Visual Studio中从事一个项目,它总是给我带来错误,外观。
using UnityEngine;
using UnityEditor.Build.Player;
public class Obsticle : MonoBehaviour {
public int damage = 1;
public float speed;
private void Update()
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player")) {
other.GetComponent<Player>().health -= damage;
Debug.Log(other.GetComponent<Player>().health);
Destroy(gameObject);
}
}
}
答案 0 :(得分:0)
首先,建立自己的玩家类会导致冲突。换课 名称,也不要使用字符。大声笑。我用盖伊。播放器标记很好,并且已固定。还:
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.Tag=="Player") {
other.GetComponent<Guy>().health -= damage;
Debug.Log(other.GetComponent<Guy>().health);
Destroy(gameObject);
}
}
这是我的接听类OnTrigger方法之一,
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
//destroy 'obsticle'
Destroy(this.gameObject);
}
}