I am a beginner to unity and I had recently tried to add knock back if hit by an enemy but I get an error message of: Assets\Scripts\Enemy.cs(11,13): error CS0246: The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?)
I know it is because of my private Player player; but I don't know how to fix this, please help me...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public int maxHealth = 100;
int currentHealth;
private Player player;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent();
currentHealth = maxHealth;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
//Play hurt anmiation
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
Debug.Log("Enemy Died!");
//Die Animation
//Diasable the enemy
GetComponent().enabled = false;
this.enabled = false;
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("Player"))
{
StartCoroutine(player.Knockback(0.02f, 350, player.transform.position));
}
}
}
↧