Caling a GameManager function doesn't work

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?

By understanding where it goes wrong. Debugging is the tool of choice, also logs.

Set a breakpoint in the trigger enter method. This may not get called or the tag isn’t what you expect.

So i set a few breakpoints as u said and found out that this stroke doesn’t work:

player.PlayerHp += 1;

Do you know why PlayerHp doesn’t change? cause im new and cannot figure it out by myself…

Well, simply add a print within the AddHp() method and make some kills. If you never get anything printed to the console that means the method is never called so the problem doesn’t come from the instruction:

player.PlayerHp += 1;

It you see your message in the console then you have a problem accessing your player reference.
This is what @CodeSmile told you to try, you add what we call breakpoints. Start simple and just write something like:

Debug.Log("Here");

If "Here" shows up in the console, you reach the instruction and you can now move on to the next layer. Hopefully it is clear enough for a beginner, if not, feel free to ask for more explanations.

My bad, have troubles with explaing. I did like you said and the debug message shows in console, but the PlayerHp doesn’t change. Do you know how do i access my player reference?

Alright. What I meant by your player reference is the instruction player.PlayerHp. If you can see a message coming from the method where this instruction is but the incrementation (the += 1) doesn’t work, then there is not a lot of things that could go wrong. The incrementation itself will never fail for an int, so if PlayerHp doesn’t get updated, the problem comes from player.PlayerHp. Maybe the variable player isn’t set up properly… honestly there could be a 1000 things that are not working in your code, but by just reading it we can’t really help you more than what we are doing right now. You really need to learn how to debug your own code, especially as a beginner. Think of it as trying to solve a puzzle, you try to see which instruction gets executed, once you are sure something is working, you go to another layer where that thing is being used, and you start your investigation again, etc… until you find the culprit.

got it, thank you)

1 Like

No problem. You should mark you thread as solved then :slightly_smiling_face:.