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