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

时间:2016-08-17 09:11:58

标签: unity3d unityscript

当玩家杀死一名敌人时,我想获得10分,但我没有得到它。我已经创建了一种在玩家的PuntosPistas脚本中添加分数的方法,只需调用敌人的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 damage)(在Assets / Invector-3rdPersonController / Scripts / CharacterAI / v_AIMotor.cs:582) UnityEngine.Component:SendMessage(String,Object,SendMessageOptions) vMeleeManager:OnDamageHit(HitInfo)(在Assets / Invector-3rdPersonController / Scripts / MeleeCombat / vMeleeManager.cs:295) UnityEngine.Component:SendMessageUpwards(String,Object,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)

1 个答案:

答案 0 :(得分:1)

请调查您调用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();
}
}

无效更新不是最好的方法,但应该做好工作。

相关问题