簡體   English   中英

需要對象引用才能訪問C#中的非靜態字段

[英]An object reference is required to access non-static field in C#

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)

我正在Unity中制作第一個Person Shooter游戲,如果可能的話,請告訴我有關我上面編寫的玩家死動畫員代碼的更多建議。

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

我已經更正了代碼。 它是利用一個簡單的錯誤PlayerHealth ,而不是你的意思這是playerHealth 使用大寫字母是指類本身。 小寫字母將引用您在此類之前定義的當前對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM