Unity:PlayerLife 变量在 Inspector 或 HUD 中没有明显更新,但在后台仍然有效

Unity: PlayerLife variable doesn't update visibly in Inspector or HUD, but still functional in Background

playerLife 变量在检查器或屏幕上的健康文本中没有明显更新,但 Player 仍然死亡,因为 playerLife 降至零以下。

我确定附加到 Zombie GameObject 的 Player 预制件只是 Player 预制件,而不是场景中活动的 Player。我如何使僵尸始终通过脚本引用场景中的活动玩家而不是基本的玩家预制件? (此外,它不允许我手动将活动玩家拖入僵尸)

Call hierarchy for playerLife

public class Player : MonoBehaviour
{
    public RaycastHit hit;
    public int gunDamage = 1;
    public Zombie zombie;

    private float hitForce = 100f;
    public float playerLife;
    private Vector3 flareLower = new Vector3(0, -0.5f, 0);

    void Start()
    {
        spawnPoints = playerSpawnPoint.GetComponentsInChildren<Transform>();
        playerLife = 200;
    }

    void Update() //T-toggle
    {
        if (Input.GetButton("Fire1"))
        {
            LazerBeam();
        }

        if (reSpawn != lastToggle)
        {
            ReSpawn();
            reSpawn = false;
        }
        else
            lastToggle = reSpawn;
    }



    public void Life (float damage)
    {
        playerLife -= damage;
        if (playerLife <=0)
        {
            playerLife = 100;
            SceneManager.LoadScene(2);
        }
    }

}

public class Zombie : MonoBehaviour
{
    public int currentHealth;
    public Player player;
    public PlayerLifeCollider playerCollider; 
    private int damage;

    public void Damage(int damageAmount)
    {
        currentHealth -= damageAmount;
        if (currentHealth <= 0)
        {
            PlayerLifeCollider.instance.ObjectsInRange.Remove(gameObject);
            DestroyZombie();
        }
    }

    public void DestroyZombie()
    {
        Destroy(gameObject);
        // gameObject.SetActive(false);
    }

    public void DamagePlayer(float damage)
    {
         player.Life(damage);
    }
}
public void Damage(int damageAmount)
{
    currentHealth -= damageAmount;

    print(currentHealth);// will show in terminal if thats what you are asking


    if (currentHealth <= 0)
    {
        PlayerLifeCollider.instance.ObjectsInRange.Remove(gameObject);
        DestroyZombie();
    }
}

如您所说,问题在于您没有在场景中引用 Player 对象,而是引用预制对象。为避免这种情况,您可以向 Zombie 脚本添加一个 Start 函数,并要求查找场景中唯一的 Player 实例。为此,您可以使用 FindObjectOfType 函数:

void Start()
{
    player = FindObjectOfType<Player>();
}

考虑到整个场景中只有一个 Player 脚本,您还可以在 Player class 中保存对 Player 实例的静态引用。

public class Player : MonoBehaviour
{
    private static Player _instance;

    public static Player Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType<Player>();
            }
           return _instance;
        }
    }

    // Reset of your class

}

然后您可以在 Zombie 脚本中获取此引用:

public class Zombie : MonoBehaviour
{
    static Player player;

    void Start()
    {
        if(player == null)
        {
            player = Player.Instance;
        }
    }

    // Rest of your class content
}

这样一来,您将只调用一次 FindObjectOfType 函数,而不是使用 Zombie 脚本对每个对象调用一次。