GetComponentInChildren









How do I use Get Component In Children
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: PlayerDiamond.cs Copy
28     {
29         get
30         {
31             if( m_DiamondRenderer == null )
32             {
33                 m_DiamondRenderer = GetComponentInChildren();
34             }
35
36             return m_DiamondRenderer;
37         }
38     }
File name: BlockScript.cs Copy
94  public void setBlockNumber(int blockNumber) {
95   this.blockNumber = blockNumber;
96   TextMesh textMesh = this.GetComponentInChildren();
97   Material blockTextMaterial = gameObject.transform.Find ("BlockText").gameObject.GetComponent().material;
98   MeshRenderer cube = gameObject.GetComponentInChildren();
99   Color cubeColor = cube.GetComponent().material.color;
100
101   //block doesn't exits
102   if(blockNumber == -2) {
103    cube.GetComponent().enabled = false;
104    textMesh.text = "";
105   }
106   //block is empty
107   else if (blockNumber == -1 ) {
108    cube.GetComponent().enabled = false;
109    cube.GetComponent().material.color = new Color(1, 1, 1, 0.2f);
110    textMesh.text = "";
111   }
112   //block has a value
113   else {
114    cube.GetComponent().enabled = true;
115    cube.material.color = this.getColor (blockNumber);
116    blockTextMaterial.SetColor ("_Color", new Color(1,1,1));
117    if(blockNumber == 0) blockTextMaterial.SetColor ("_Color", new Color(0.8f,0.8f,1));
118    textMesh.text = blockNumber.ToString();
119    transform.position = this.originalPosition;
120   }
121
122  }
File name: Pools.cs Copy
76     public void AcivateBoss()
77     {
78         bossSystem.SetActive(true);
79         bossSystem.transform.position = new Vector3(Camera.main.transform.position.x, 0, Camera.main.transform.position.z);
80         bossSystem.GetComponentInChildren().Activation();
81     }
File name: UIVerticalScroller.cs Copy
122         public void Update()
123         {
124             if (_arrayOfElements.Length < 1)
125             {
126                 return;
127             }
128
129             for (var i = 0; i < elementLength; i++)
130             {
131                 distReposition[i] = _center.GetComponent().position.y - _arrayOfElements[i].GetComponent().position.y;
132                 distance[i] = Mathf.Abs(distReposition[i]);
133
134                 //Magnifying effect
135                 float scale = Mathf.Max(0.7f, 1 / (1 + distance[i] / 200));
136                 _arrayOfElements[i].GetComponent().transform.localScale = new Vector3(scale, scale, 1f);
137             }
138             float minDistance = Mathf.Min(distance);
139
140             for (var i = 0; i < elementLength; i++)
141             {
142                 _arrayOfElements[i].GetComponent().interactable = false;
143                 if (minDistance == distance[i])
144                 {
145                     minElementsNum = i;
146                     _arrayOfElements[i].GetComponent().interactable = true;
147                     result = _arrayOfElements[i].GetComponentInChildren().text;
148                 }
149             }
150
151             ScrollingElements(-_arrayOfElements[minElementsNum].GetComponent().anchoredPosition.y);
152         }
File name: ScrollSnap.cs Copy
86  public void ShiftLayoutElement() {
87   Destroy(GetComponentInChildren().gameObject);
88   SetContentSize(LayoutElementCount() - 1);
89   cellIndex -= 1;
90   content.anchoredPosition = new Vector2(content.anchoredPosition.x + cellSize.x, content.anchoredPosition.y);
91  }
File name: ScrollSnap.cs Copy
195  void WrapElementAround() {
196   if(cellIndex <= 0) {
197    var elements = content.GetComponentsInChildren();
198    elements[elements.Length - 1].transform.SetAsFirstSibling();
199    cellIndex += 1;
200    content.anchoredPosition = new Vector2(content.anchoredPosition.x - cellSize.x, content.anchoredPosition.y);
201   } else if(cellIndex >= CalculateMaxIndex()) {
202    var element = content.GetComponentInChildren();
203    element.transform.SetAsLastSibling();
204    cellIndex -= 1;
205    content.anchoredPosition = new Vector2(content.anchoredPosition.x + cellSize.x, content.anchoredPosition.y);
206   }
207  }

GetComponentInChildren 149 lượt xem

Gõ tìm kiếm nhanh...