2017-03-12 64 views
0
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class EnemyAttack : MonoBehaviour { 
    public float timeBetweenAttacks = 0.5f; 
    public int attackDamage = 10; 
    Animator anim; 
    GameObject Player; 
    PlayerHealth playerHealth; 

    //EnemyHeath enemyhealth; 

    bool playerInRange; 
    float timer; 
    private Animator animator = null; 

    void Awake() { 
     Player = GameObject.FindGameObjectWithTag ("Player"); 
     playerHealth = Player.GetComponent<PlayerHealth>(); 

     //enemyHealth = GetComponent<EnemyHealth>(); 

     anim = GetComponent <Animator>(); 
    } 


    void Attack(Collider other) { 
     if (other.gameObject == Player) { 
      playerInRange = true; 
      animator.SetBool ("idle0ToAttack1", true); 
     } 
    } 

    void Attack1(Collider other) { 
     if (other.gameObject == Player) { 
      playerInRange = false; 
     } 
    } 

    void Update() { 
     timer +=Time.deltaTime; 

     if (timer >= timeBetweenAttacks /*&& enemyHealth.currentHealth > 0*/) { 
      AttackPlayer(); 
     } 

     if (playerHealth.currentHealth <= 0) { 
      Destroy (this.Player); 
     } 
    } 

    void AttackPlayer() { 
     timer = 0f; 
     if (PlayerHealth.currentHealth > 0) { 
      playerHealth.TakeDamage (attackDamage); 
     } 
    } 
} 

錯誤發生在最後一個方法void AttackPlayer() if(PlayerHealth.currentHealth > 0)需要對象引用來訪問C#中的非靜態字段

我正在Unity中製作第一人稱射擊遊戲,如果可能的話,請告訴我爲上面寫的玩家死亡動畫代碼提供更多建議。

+0

'PlayerHealth'是一類。您無法在類上調用實例方法。你需要在一個類(一個對象)的實例中調用它。否則,你期望'PlayerHealth.currentHealth'指什麼?哪位球員的健康? – Alexander

回答

0
void AttackPlayer() 
{ 
    timer = 0f; 
    if(playerHealth.currentHealth > 0) 
    { 
     playerHealth.TakeDamage(attackDamage); 
    } 
} 

我已更正了代碼。這是一個簡單的錯誤,使用PlayerHealth而不是你的意思是playerHealth。使用大寫字母是指這個類本身。小寫字母表示您在本課程前面定義的當前對象。

相關問題