简体   繁体   中英

The type or namespace name `Player' could not be found

PLs, i am working on aproject in unity and visual studio and it keeps giving me error, look.

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);
        }
    }
}

first of all making your own player class causes conflicts. change the class name, dont use character either. lol. i use Guy. the player tag is fine, and recomeended. also:

void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.Tag=="Player") {

            other.GetComponent<Guy>().health -= damage;
            Debug.Log(other.GetComponent<Guy>().health);
            Destroy(gameObject);
        }
    }

this is one of my pickup class OnTrigger methods,

private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {

            //destroy 'obsticle'
            Destroy(this.gameObject);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM