unity公共变量未与gameObject.find

时间:2018-10-31 12:36:11

标签: c# variables unity3d

我这里有一个名为EnemyHealth的脚本和一个名为DatabaseManager的脚本。在DatabaseManager脚本中,我试图从EnenmyHealth脚本中引用一个新创建的公共int deathCount变量,但是当所有其他变量都在显示时,该公共变量对我不显示,这是代码

EnemyHealth

using UnityEngine;

namespace CompleteProject
{
    public class EnemyHealth : MonoBehaviour
    {
        public int startingHealth = 100;            // The amount of health the enemy starts the game with.
        public bool isDead;                                // Whether the enemy is dead.
        public int deathCount;
        public int currentHealth;                   // The current health the enemy has.
        public float sinkSpeed = 2.5f;              // The speed at which the enemy sinks through the floor when dead.
        public int scoreValue = 10;                 // The amount added to the player's score when the enemy dies.
        public AudioClip deathClip;                 // The sound to play when the enemy dies.

DatabaseManager

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class DatabaseManager: MonoBehaviour
{
    private void Awake()
    {
        int x = GameObject.Find("ZomBear").GetComponent<EnemyHealth>().deathCount;
    }

在所有其他公共变量都可见的情况下,似乎找不到该变量

2 个答案:

答案 0 :(得分:3)

EnemyHealth位于CompleteProject名称空间内:您应该添加

using CompleteProject 

DatabaseManager脚本的顶部,以使该名称空间具有可见性。

答案 1 :(得分:0)

在您的IDE中,点击全部保存。确保在编辑多个脚本时,保存所有涉及脚本的nw版本。听起来很简单,但我已经做过数百次了,通常只需一秒钟即可实现。