So i trying to make an ability system and for a test decided to add a player 1 hp for everu 10 kills.
GameManager code:
public class GameManager : MonoBehaviour
{
public Player player;
public static GameManager Instance;
[HideInInspector]
public int killed = 0;
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
public void AddKill()
{
killed++;
if (killed >= 10)
{
killed = 0;
AddHp();
}
}
public void AddHp()
{
player.PlayerHp += 1; //TODO not working
}
}
Enemy code:
public class enemy : MonoBehaviour
{
[SerializeField]
private GameManager gameManager;
[SerializeField]
private Transform target;
public float speed;
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, target.position, speed*Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D outro)
{
if (outro.gameObject.tag != "Enemy")
{
gameManager.AddKill();
Destroy(gameObject);
}
}
}
I’ve made a lot of testes and found out that the kills are not counting. But to be sure here is the player code:
using UnityEngine;
public class Player : MonoBehaviour
{
public Camera cam;
public Rigidbody2D rb;
public GameManager gameManager;
Vector2 mousePos;
public int PlayerHp;
[SerializeField]
private GameObject deadScreen_prefab;
[SerializeField]
private GameObject spawner_prefab;
void Update()
{
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
if (PlayerHp == 0)
{
Destroy(gameObject);
Destroy(GameObject.FindWithTag("Respawn"));
Instantiate(deadScreen_prefab);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Enemy")
PlayerHp -= 1;
}
private void FixedUpdate()
{
Vector2 mouseDirection = mousePos - rb.velocity;
float angle = Mathf.Atan2(mouseDirection.y, mouseDirection.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
}
How do i make it work?