团结奋斗使无尽的亚军游戏2D

时间:2019-08-13 22:07:52

标签: c# unity3d communityengine

嗨,我正在尝试在Unity Engine中构建无尽的运行器运行错误,并且不断收到此错误消息。 找不到类型或名称空间名称“ Player”(您是否缺少using指令或程序集引用)

这是我的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class obsticale : 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")) {
            //plaer takes damage!
            other.GetComponent<Player>().health -= damage;
            Debug.Log(other.GetComponent<Player>().health);
            Destroy(gameObject);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您有错别字。

应该为other.gameObject.CompareTag("Player")

下面还有其他多种错别字。

void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Player"))) 
    {
        //player takes damage!
        player = other.gameObject;

        player.GetComponent<yourscriptname>().health -= damage;

        Debug.Log(player.GetComponent<yourscriptname>().health);

        Destroy(player);
    }

编辑:我也了解您是新手,但是我相信人们会希望您将代码放入网站上的指定代码块中,以使我们更轻松地为您提供帮助。