The type or namespace name 'Player' could not be found, how can i fix it?

I am making a Top-down 2D shooter for a game jam that i'm participating, but when i did the Enemy script for it to damage the player, i got this error : The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?) Here is the line of code that i'm having problems with:

Line 9

    private Player player;

Line 13

 player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();

And here is the full script:

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

public class Enemy : MonoBehaviour
{
    public float speed;
    private Transform playerPos;
    private Player player;

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
        playerPos = GameObject.FindGameObjectWithTag("Player").transform;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = Vector2.MoveTowards(transform.position, playerPos.position, speed * Time.deltaTime);
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.CompareTag("Player"))
        {
            player.health--;
            Destroy(gameObject);
        }
        if(other.CompareTag("Projectile"))
        {
            Destroy(gameObject);
        }
    }
}

What is 'Player'?
Do you have an Component/script called Player.cs in your scene?
If it's a component in your scene, try using GameObject player;

1 Like

Create a New Script and Add paste the code below:

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

public class Player : MonoBehaviour
{
    // Start is called before the first frame update
    public float speed;
    private Rigidbody2D rigidbody;

    void Start()
    {
        rigidbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        rigidbody.velocity = new Vector2(speed, rigidbody.velocity.y);
    }
}

7311820--886465--Screenshot (110).png