如何引用从孩子到父母的课程?

这可能听起来很愚蠢,但我如何从父母的另一个脚本中的一个脚本中引用一个类? 我在谷歌上找不到任何东西。 注意:我的脚本中有几个错误,这不是post的重点。

//Public //Private private Rigidbody myRigidbody; private Renderer myRenderer; private Material tileDefaultMaterial; private Material tileSelectedMaterial; private Material tileSameGroupMaterial; void Start () { myRigidbody = GetComponent (); myRenderer = GetComponent (); 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 ().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; } } 

有句名言:

 var allTiles = transform.GetComponentsInChildren(); 

正如我昨天告诉你的那样,在Tile.cs中添加OnMouseDown()并编写myRenderer.material = tileDefaultMaterial; 那里。 无需在TileManager.cs中编写此代码。 使用OnMouseDown()时无需使用Raycast

我无法读取您的图像代码,因此我将为该示例构建自己的类名。 我将调用父类TileManager和子类Tile

Tile说你想要访问TileManager的tile数组。 如果allTiles被声明为public,你可以在某个地方做Tile

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

现在, Tile子对数组有一个引用。 这是你想要的吗?

怎么样base.Method?

应该这样做