2013-07-01 53 views
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()」將拋出一個錯誤-
「方法是無法訪問由於其保護級別」

我需要與幫助......

+0

的可能重複的[不可訪問由於其保護水平(http://stackoverflow.com/questions/訪問6125077 /由於其保護級別而無法訪問)或者其他類似問題中的任何一個... –

回答

3

既然你不定義任何訪問修飾符,方法HealthRulse是專用的,所以你不能從訪問外EnemyHealth

默認情況下,類成員和結構成員(包括嵌套類和結構成員)的訪問級別是私有的。私人嵌套類型不是從包含類型

更改定義

public void HealthRulse(int adj) 
相關問題