I have a two an "enemy" sprite that when the "player" sprite touches, should cause damage.
The PlayerStatus script just holds information about the players health. When the EnemyAIController OnTriggerEnter2D method is fired, I wanted to call a method in the PlayerStatus script that would reduce the health by x amount.
I made the TakeDamage method accessor public with a static modifier. So I could call PlayerStatus.TakeDamage(float x)
, but that doesn't work.
I get the error: An object reference is required to access non-static member `PlayerStatus.TakeDamage(float)'
I didn't want to use findObjectByTag
because I have read that is slow and a lazy way of doing it.
Am I missing something?
PlayerStatus Script:
public class PlayerStatus : MonoBehaviour {
public float health = 3.0f;
public static void TakeDamage(float damage){
health -= damage;
}
}
EnemyAIController Script:
public class EnemyAIController : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other) {
Debug.Log("Reduce Player Health");
PlayerStatus.TakeDamage (1.0f);
}
}