当玩家杀死敌人时获得积分

Gain points when the player kill an enemy

我想在玩家杀死敌人时获得10分,但我没有得到。我已经创建了一个方法来在播放器中的 PuntosPistas 脚本上添加分数,并且只需在 Enemy 的 CheckHealth 中调用该方法,但它的总分不会达到 10 分。有谁能够帮助我?这是我的代码:

在敌人中:

public class v_AIMotor : vCharacter
{
    GameObject Player;

    void Start ()
    {
        Player = GameObject.FindGameObjectWithTag("Player");
    }

    public void CheckHealth()
    {
        if (currentHealth <= 0 && !isDead)
        {

            isDead = true;
            Player.GetComponent<PuntosPistas>().KillEnemy ();
            print("10 points”);
            DisableActions();
        }
    }
}

在播放器中:

public class PuntosPistas : MonoBehaviour 
{
    public int Score;
    public Text TextoContador;

    void Start ()
    {
        Score = PlayerPrefs.GetInt("Score");
        TextoContador.text = "" + Score;
    }

    public void KillEnemy()
    {
        Score = Score + 10;
        TextoContador.text = "" + Score;
        PlayerPrefs.SetInt("Score",Score);
    }
}

对不起!这是我调用 CheckHealth():

的完整代码
 #region AI Health

    GameObject Player;

    void Start ()
    {
        Player = GameObject.FindGameObjectWithTag("Player");
    }

    public void CheckHealth()
    {
        if (currentHealth <= 0 && !isDead)
        {
            isDead = true;
            Player.GetComponent<PuntosPistas> ().KillEnemy ();
            DisableActions();
        }
    }

    public void HealthRecovery()
    {
        if (currentHealth <= 0) return;
        if (currentHealthRecoveryDelay > 0)
        {
            currentHealthRecoveryDelay -= Time.deltaTime;
        }
        else
        {
            if (currentHealth > maxHealth)
                currentHealth = maxHealth;
            if (currentHealth < maxHealth)
                currentHealth = Mathf.Lerp(currentHealth, maxHealth, healthRecovery * Time.deltaTime);
        }
    }

    protected void RemoveComponents()
    {
        if (_capsuleCollider != null) Destroy(_capsuleCollider);
        if (_rigidbody != null) Destroy(_rigidbody);
        if (animator != null) Destroy(animator);
        if (agent != null) Destroy(agent);
        var comps = GetComponents<MonoBehaviour>();
        foreach (Component comp in comps) Destroy(comp);
    }

    public override void TakeDamage(Damage damage)
    {
        if (rolling || currentHealth <= 0) return;
        if (canSeeTarget() && !damage.ignoreDefense && !actions && CheckChanceToRoll()) return;
        // change to block an attack
        StartCoroutine(CheckChanceToBlock(chanceToBlockAttack, 0));
        // defend attack behaviour
        if (canSeeTarget() && BlockAttack(damage)) return;
        // instantiate hit particle
        var hitRotation = Quaternion.LookRotation(new Vector3(transform.position.x, damage.hitPosition.y, transform.position.z) - damage.hitPosition);
        SendMessage("TriggerHitParticle", new HittEffectInfo(new Vector3(transform.position.x, damage.hitPosition.y, transform.position.z), hitRotation, damage.attackName), SendMessageOptions.DontRequireReceiver);
        // apply damage to the health
        currentHealth -= damage.value;
        currentHealthRecoveryDelay = healthRecoveryDelay;
        // apply tag from the character that hit you and start chase
        if (!sphereSensor.passiveToDamage && damage.sender != null)
        {
            target = damage.sender;
            currentState = AIStates.Chase;
            sphereSensor.SetTagToDetect(damage.sender);
            if (meleeManager != null)
                meleeManager.SetTagToDetect(damage.sender);
        }
        // trigger hit sound
        if (damage.sender != null)
            damage.sender.SendMessage("PlayHitSound", SendMessageOptions.DontRequireReceiver);
        // update the HUD display
        if (healthSlider != null)
            healthSlider.Damage(damage.value);
        // trigger the HitReaction when the AI take the damage
        var hitReactionConditions = stepUp || climbUp || jumpOver || quickTurn || rolling;
        if (animator != null && animator.enabled && !damage.activeRagdoll && !hitReactionConditions)
        {
            animator.SetInteger("Recoil_ID", damage.recoil_id);
            animator.SetTrigger("HitReaction");
        }
        // turn the ragdoll on if the weapon is checked with 'activeRagdoll'
        if (damage.activeRagdoll)
            transform.SendMessage("ActivateRagdoll", SendMessageOptions.DontRequireReceiver);

        CheckHealth();
    }

    #endregion

这是控制台中的错误:

NullReferenceException: 对象引用未设置到对象的实例 Invector.v_AIMotor.CheckHealth () (位于 Assets/Invector-3rdPersonController/Scripts/CharacterAI/v_AIMotor.cs:504) Invector.v_AIMotor.TakeDamage (.Damage 伤害) (at Assets/Invector-3rdPersonController/Scripts/CharacterAI/v_AIMotor.cs:582) UnityEngine.Component:SendMessage(字符串,对象,SendMessageOptions) vMeleeManager:OnDamageHit(HitInfo)(位于 Assets/Invector-3rdPersonController/Scripts/MeleeCombat/vMeleeManager.cs:295) UnityEngine.Component:SendMessageUpwards(字符串,对象,SendMessageOptions) Invector.vHitBox:CheckHitProperties(Collider)(在 Assets/Invector-3rdPersonController/Scripts/MeleeCombat/vHitBox.cs:68) Invector.vHitBox:OnTriggerEnter(Collider)(在 Assets/Invector-3rdPersonController/Scripts/MeleeCombat/vHitBox.cs:48)

请调查您调用 CheckHealth() 的方式,从您的代码中看不清楚。 如果您正确调用该方法,该代码应该可以工作,请尝试以下方式:

    public class v_AIMotor : vCharacter{
GameObject Player;

void Start ()
{
    Player = GameObject.FindGameObjectWithTag("Player");
}

public void CheckHealth()
{
    if (currentHealth <= 0 && !isDead)
    {

        isDead = true;
        Player.GetComponent<PuntosPistas>().KillEnemy ();
        print("10 points”);
        DisableActions();
    }
}

void Update()
{
    CheckHealth();
}
}

void update 肯定不是最好的方法,但应该可以做到。