【问题标题】:Method is inaccessible due to its protection level方法由于其保护级别而无法访问
【发布时间】:2013-07-01 15:26:02
【问题描述】:

我正在使用 Unity3d 和 C#,并且我有两个脚本:

脚本 1:

using UnityEngine;
using System.Collections;

public class PlayerAttack : MonoBehaviour {

    public GameObject target;

    // Update is called once per frame
    void Update () {

        if(Input.GetKeyUp(KeyCode.F))
        {
            Attack();
        }
    }
     void Attack() {
        EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
        eh.HealthRulse(-10);
    }

}

脚本 2:

using UnityEngine;
using System.Collections;

public class EnemyHealth : MonoBehaviour {
public int curHealth = 100;
public int maxHealth = 100;
public float healthBarLeangth;
    // Use this for initialization
    void Start () {
    healthBarLeangth = Screen.width / 2;
    }

    // Update is called once per frame
    void Update () {
         HealthRulse(0);
    }
    void OnGUI() {
        GUI.Box(new Rect(10,40,Screen.width / 2 / (maxHealth / curHealth),20),curHealth + "/" + maxHealth);
    }
    void HealthRulse(int adj){
        if ( curHealth < 0)
            curHealth = 0;
        if (curHealth > maxHealth)
            curHealth = maxHealth;
        if(maxHealth < 1)
            maxHealth = 1;

        curHealth += adj;
        healthBarLeangth = (Screen.width / 2) * (curHealth / (float)maxHealth);
    }
}

“脚本 2”中定义并由 GetComponent 在“脚本 1”中调用的函数“HeathRulse()”抛出错误-
"方法由于其保护级别而无法访问"

我需要帮助...

【问题讨论】:

标签: c# unity3d


【解决方案1】:

由于您没有定义任何访问修饰符,方法 HealthRulse 是私有的,因此您无法从 EnemyHealth 类外部访问它

类成员和结构成员(包括嵌套类和结构)的访问级别默认为私有。私有嵌套类型不能从包含类型外部访问

将定义改为

public void HealthRulse(int adj)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-26
    • 2011-09-01
    • 1970-01-01
    • 2011-04-02
    • 2011-02-07
    • 2012-01-25
    相关资源
    最近更新 更多