Weird behavior, do you know how to fix it?

So, I have a code for a player:

using System.Diagnostics;
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()
    {
        if (playerHp != 2)
            UnityEngine.Debug.Log("Hp rearranged");
        mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Enemy")
            playerHp -= 1;
        if (playerHp == 0)
        {
            Destroy(gameObject);
            Destroy(GameObject.FindWithTag("Respawn"));
            Instantiate(deadScreen_prefab);
        }
    }

    private void FixedUpdate()
    {
        Vector2 mouseDirection = mousePos - rb.velocity;
        float angle = Mathf.Atan2(mouseDirection.y, mouseDirection.x) * Mathf.Rad2Deg - 90f;
        rb.rotation = angle;
    }

    public void AddPlHp()
    {
        UnityEngine.Debug.Log("Add player's hp");
        playerHp += 1;
    }
}

The thing is this part works as it needed and changes the playerHp:

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Enemy")
            playerHp -= 1;
        if (playerHp == 0)
        {
            Destroy(gameObject);
            Destroy(GameObject.FindWithTag("Respawn"));
            Instantiate(deadScreen_prefab);
        }
    }

BUT this part doesn’t change when the function is called:

public void AddPlHp()
{
    UnityEngine.Debug.Log("Add player's hp");
    playerHp += 1;
}

Do you know how to fix it?

Does it say the “Add player’s hp” bit in Debug? Or just nothing? If nothing, something somewhere else in your code is blocking it. A quick way to check if the code is firing is to call the method in update.

BTW, you may also want to add:

if (playerHP > playerMaxHp) { playerHP = playerMaxHp; }

Yes, it says “Add player’s hp”. Thanks for the addition, will add it after fixing the main problem

Okay. If it’s doing that in update and in normal play, then something else is constantly setting the value to a set number. Try using Ctrl F, set it to ‘entire solution’, and put playerHP in it to see if anything else is trying to interact with it.

BTW might want to check my previous note as I added to it.

Just tried it, the playerHp is used only in this file :sob:

Also have this Game Manager code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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()
    {
        Debug.Log("Killed function");
        killed++;
        if (killed == 1)
        {
            killed = 0;
            AddHp();
        }
    }

    public void AddHp()
    {
        Debug.Log("AddHp");
        player.AddPlHp();
    }
}

At first i tried this:

public void AddHp()
    {
        Debug.Log("AddHp");
        player.playerHp += 1;
    }

It didn’t work so i managed to rearrange it
Maybe i create a “new” player? im just new to this, do i make it right?

This is pretty odd to be honest. Maybe whatever code you have that’s reading the number is wrong. Like, say, the health bar? Or counter? If it’s not connected to anything, it’s never going to update. A quick way to check:

Debug.Log("Player Health is currently " + playerHP);

Often the best thing to do is think through the entire chain of logic that needs to go off to make something work. So:

  1. The method needs to trigger.
  2. The line about adding health needs to be reached
  3. The counter that reads the health needs to update.
  4. The counter needs to be plugged into the display you intend to use.

Very easy to forget your health bar is a placeholder!

2 Likes

Most likely you dragged the prefab into the field player when you meant to drag or assign the instance.

Whether this turns out to be your issue or not, always use better naming to guard against this.

Here’s more reading:

Instancing and prefabs in Unity and naming your variables:

If you name your public / serialized fields (variables) as simply “thing” then you set yourself up for confusion.

If something is a prefab, name it that, such as public GameObject ThingPrefab; and only drag prefabs into it. Prefabs are “dead things (assets on disk) waiting to be Instantiated.”

If something is already in the scene or you Instantiate<T>(); it in code, then store the result in a properly-named variable such as private GameObject ThingInstance;

Naming is hard… name things WELL to reduce your own confusion and wasted time.

Yes, i dragged a prefab, but i thought this is how things done… Like dragging a player prefab on a GameManager prefab… Is it correct?

Thank you for help, now i know that the code is right, the next step is to find where in Unity i messed up


So this is how it looks in Unity. Is there something you think is odd or unright?

It is correct if you only intend to Instantiate things from it.

It is incorrect if you want to do something to the Instantiate-ed thing.

The way you are doing it now, your player is running around with one health, the asset on disk is the other health you are counting down.

Those are two different variables.

A hacky fix is to just assign what comes out of Instantiate() back into that same field at runtime.

A better fix is to do it properly with two properly-named variables, as identified in my post above.