2016-11-11 228 views
3

這可能聽起來很愚蠢,但我怎麼能從父母的另一個腳本中的孩子的一個腳本中引用一個類?我無法在谷歌上找到任何東西。注意:我的腳本中有幾個錯誤,這不是帖子的要點。如何從小孩到父母引用一個類?

//Public 

//Private 
private Rigidbody myRigidbody; 
private Renderer myRenderer; 
private Material tileDefaultMaterial; 
private Material tileSelectedMaterial; 
private Material tileSameGroupMaterial; 

void Start() { 
    myRigidbody = GetComponent<Rigidbody>(); 
    myRenderer = GetComponent<Renderer>(); 
    tileDefaultMaterial = Resources.Load ("TileDefault", typeof(Material)) as Material; 
    tileSelectedMaterial = Resources.Load ("TileSelected", typeof(Material)) as Material; 
    tileSameGroupMaterial = Resources.Load ("TileSameGroup", typeof(Material)) as Material; 
} 

void Update() { 

} 

public class TileClass { 

    public int tileGroup = 0; //Indicates the Tile Group Number. 

} 

//Public 
public GameObject[] allTiles; //Aray of all Tile GameObject. 
public bool tileIsSelected = false; //If a Tile is Selected. 
public int selectedTileGroup = 0; //Indicates the Selected Tile Group Number. 
public int tileGroup = 0; //Indicates the Tile Group Number. 



//Private 


void Start() { 

    allTiles = new GameObject[transform.childCount]; 
    for(int i = 0; i < transform.childCount; i++){ 
     allTiles [i] = transform.GetChild (i).gameObject; 
    } 
} 

void Update() { 

} 

void OnMouseDown(){ 

    RaycastHit hitInfo = new RaycastHit(); 
    bool hit = Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hitInfo); 

    if (hitInfo.transform.gameObject.tag == "Tile" && tileIsSelected == false) { 
     Debug.Log ("A Tile is Selected!"); 
     tileIsSelected = true; 
     selectedTileGroup = ; 
     for(int i = 0; i < allTiles.Length; i++){ 
      if (this.tileGroup == selectedTileGroup) { 
       allTiles [i].GetComponent<Renderer>().material = tileSameGroupMaterial; 
      } 
     } 
     myRenderer.material = tileSelectedMaterial; 
    } else if (hitInfo.transform.gameObject.tag == "Tile" && tileIsSelected == true) { 
     Debug.Log ("Second Tile is Clicked! (Should Swap them!)"); 
     myRenderer.material = tileDefaultMaterial; 
    } 
} 
+0

你不能那樣做。你的班級已經從MonoBehaviour繼承。 你想要達到什麼目的? –

+2

請不要提供代碼作爲圖像。請參閱http://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-errors – gus27

+0

我想創建一個類,併爲每個tile創建一些變量,然後通過管理腳本管理它們。我不確定這是否是最好的方式。我是C#和Unity的新手。 – N1ckGreek

回答

4

有一句名言:

var allTiles = transform.GetComponentsInChildren<Tile>(); 

正如我昨天跟你說,在Tile.cs添加OnMouseDown(),寫myRenderer.material = tileDefaultMaterial;有。無需在TileManager.cs中編寫它。當使用OnMouseDown()時,不需要使用Raycast

+0

我試過讓我的代碼在Tile.cs中,但我無法管理像tileIsSelected或selectedTileGroup這樣的事情,因爲我的所有瓷磚都有這個腳本。例如,如果我選擇一個瓷磚,然後點擊空白空間,我希望tileIsSelected在所有瓷磚上都是假的,或者當我點擊組編號爲1的瓷磚時,我想更改所有瓷磚上具有相同組編號的材質。我的事情應該更容易做到這一點,如果我有TileManager.cs中的代碼。 – N1ckGreek

+0

現在有道理。 –

-4

base.Method怎麼樣?

應該這樣做

+0

帕特里克霍夫曼不編輯我的答案無用的編輯。謝謝。 –

+0

我不認爲這是一個繼承問題 - 他談論的是Unity變換層次結構中的父/子關係。 – Serlite

1

我看不懂你的形象代碼,所以我會彌補我自己的類名的例子。我會打電話給父級TileManager和子級Tile

說在Tile你想訪問TileManager瓷磚陣列。如果allTiles被宣佈爲公開,您可以在Tile的某個地方執行。

TileManager tileManager = transform.parent.GetComponent<TileManager>(); 
var allTiles = tileManager.allTiles; 

現在Tile孩子有一個對數組的引用。這是你想要的嗎?

相關問題