Unity 3D:具有玩家健康状况的 NullReferenceException

时间:2021-04-07 20:31:22

标签: c# unity3d nullreferenceexception

我是一名初级开发者,我刚刚开始制作自己的游戏,但在我的进步中,我陷入了创建敌人的困境。

这个脚本的目标是移动敌人,抓住玩家,最重要的是对玩家造成伤害,但在这种情况下,由于 NullReferenceException 错误,我无法在我的玩家健康和我的敌人控制器脚本之间建立链接这困扰着我。这是我遇到的唯一错误,我到处搜索以修复我的错误,但没有找到任何东西。

这是我的 EnemyController 脚本,我知道它并没有真正优化,但无论如何。 真正的问题就在 Update 函数 (if(playerHealth == null){}) 的第 49 行
我所有的脚本都已连接,这里是我为此使用的所有脚本。

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

public class EnemyContoller : MonoBehaviour
{
    public float lookRadius = 10f;
    public float maxHealth = 100f;
    public float currentHealth;
    Transform target;
    NavMeshAgent agent;
    public HealthBarScript healthBar;
    public Animator anim;
    EnemyAttack attack;
    PlayerHealth playerHealth;

    // Update is called once per frame
    void Start()
    {
        attack = GetComponent<EnemyAttack>();
        anim = GetComponent<Animator>();
        gameObject.layer = 6;
        currentHealth = maxHealth;
        healthBar.SetMaxHealth(maxHealth);
        target = PlayerManager.instance.player.transform;
        agent = GetComponent<NavMeshAgent>();
        playerHealth = GetComponent<PlayerHealth>();
    }
    
    void Update()
    {
        float distance = Vector3.Distance(target.position, transform.position);

        if(distance <= lookRadius)
        {
            anim.speed = agent.speed;
            anim.SetBool("isRunning",true);
            agent.SetDestination(target.position);
        }else{
            anim.speed = 0;
            anim.SetBool("isRunning",false);
        }

        if(distance <= agent.stoppingDistance)
        {
            if(playerHealth != null)
            {
                attack.MakeDamage(attack.damages, playerHealth);
                Debug.Log(playerHealth.currentHealth);
            }
            else if(playerHealth == null)
            {
                Debug.Log("Player Health = null");
            }
            FaceTarget();
        }
    }

    public void TakeDamage(float amount){
        currentHealth -= amount;
        healthBar.SetHealth(currentHealth);
        if(currentHealth <= 0)
        {
            Die();
        }
    }
    void Die()
    {
        Destroy(gameObject);
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, lookRadius);
    }

    void FaceTarget()
    {
        Vector3 direction = (target.position - transform.position).normalized;
        Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 3f);
    }
}

这是我的敌人攻击脚本

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

public class EnemyAttack : MonoBehaviour
{
    public float damages = 10f;
    public float damageRate = 1f;
    private float damageCooldown = 0f;
    PlayerHealth pH;

    void Update()
    {
        damageCooldown -= Time.deltaTime;
    }
    public void MakeDamage(float amount, PlayerHealth health)
    {
        if(damageCooldown <= 0f)
        {
            pH.TakeDamage(damages);
            Debug.Log(health);
            damageCooldown = 1f / damageRate;
        }
    }
}

这是我的PlayerHealth 脚本

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

public class PlayerHealth : MonoBehaviour
{
    public float currentHealth;
    public float maxHealth = 50f;
    public HealthBarScript healthBar;
    // Start is called before the first frame update
    void Start()
    {
        currentHealth = maxHealth;
        healthBar.SetMaxHealth(maxHealth);
    }

    public void TakeDamage(float amount)
    {
        currentHealth -= amount;
        healthBar.SetHealth(currentHealth);
        Debug.Log(currentHealth);
        if(currentHealth <= 0)
        {
            Debug.Log("You're Dead.");
            Time.timeScale = 0;
        }
    }
}

如果有人能帮助我那就太好了,我不知道该怎么做才能修复这个持续 2 天的错误。

0 个答案:

没有答案