2016-08-17 14 views
-1

プレイヤーが敵を殺すときに10ポイントを獲得したいが、それは得られない。 Player内の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(のインスタンスに設定されていません(アセット/ Invector-3rdPersonController/Scripts/CharacterAI/v_AIMotor.cs:582) Invector.v_AIMotor.TakeDamage(ダメージのダメージ)UnityEngine .Component:SendMessage(String、Object、SendMessageOptions) vMeleeManager:OnDamageHit(HitInfo)(アセット/ Invector-3rdPersonConで(Collider)(Asset/Invector-3rdPersonController/Scripts/MeleeCombat/vHitBox.csの場合): CheckoutProperties(Collider)(Assets/Invector-3rdPersonController/Scripts/MeleeCombat/vMeleeManager.cs:295) 68) Invector.vHitBox:OnTriggerEnter(衝突)(資産/ Invector-3rdPersonControllerで/スクリプト/ MeleeCombat/vHitBox.cs:48)

+0

ここで 'CheckHealth()'メソッドを呼び出していますか? –

+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(); 
} 
} 

のボイド更新は確かに最善の方法ではなく、仕事をする必要があります。

関連する問題