0
我使用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()」將拋出一個錯誤-
「方法是無法訪問由於其保護級別」
我需要與幫助......
的可能重複的[不可訪問由於其保護水平(http://stackoverflow.com/questions/訪問6125077 /由於其保護級別而無法訪問)或者其他類似問題中的任何一個... –