這可能聽起來很愚蠢,但我怎麼能從父母的另一個腳本中的孩子的一個腳本中引用一個類?我無法在谷歌上找到任何東西。注意:我的腳本中有幾個錯誤,這不是帖子的要點。如何從小孩到父母引用一個類?
//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;
}
}
你不能那樣做。你的班級已經從MonoBehaviour繼承。 你想要達到什麼目的? –
請不要提供代碼作爲圖像。請參閱http://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-errors – gus27
我想創建一個類,併爲每個tile創建一些變量,然後通過管理腳本管理它們。我不確定這是否是最好的方式。我是C#和Unity的新手。 – N1ckGreek