繁体   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