FindGameObjectsWithTag
How do I use Find Game Objects With Tag
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: DemoMecanimGUI.cs
Copy
42 public void FindRemoteAnimator()
43 {
44 if( m_RemoteAnimator != null )
45 {
46 return;
47 }
48
49 // the prefab has to be tagged as Player
50 GameObject[] gos = GameObject.FindGameObjectsWithTag( "Player" );
51 for( int i = 0; i < gos.Length; ++i )
52 {
53 PhotonView view = gos[ i ].GetComponent
54 if( view != null && view.isMine == false )
55 {
56 m_RemoteAnimator = gos[ i ].GetComponent
57 }
58 }
59 }
File name: GameplayController.cs
Copy
125 void ClearAllEnemies(){
126 GameObject[] enemies = GameObject.FindGameObjectsWithTag ("Enemy");
127 GameObject[] bossbullets = GameObject.FindGameObjectsWithTag ("Boss Bullet");
128 GameObject[] coins = GameObject.FindGameObjectsWithTag ("Coin");
129 GameObject boss = GameObject.FindGameObjectWithTag ("Boss");
130
131 if(boss != null){
132 Destroy (boss);
133 }
134
135 if(enemies != null){
136 foreach (GameObject enemy in enemies) {
137 Destroy (enemy);
138 }
139 }
140
141 if(bossbullets != null){
142 foreach (GameObject bossBullet in bossbullets) {
143 Destroy (bossBullet);
144 }
145 }
146
147 if(coins != null){
148 foreach (GameObject coin in coins) {
149 Destroy (coin);
150 }
151 }
152
153 }
File name: GameplayController.cs
Copy
70 void InitializeVariables(){
71 gameInProgress = true;
72 enemies = new List
73 objects = new List
74 distance = 10f;
75 if(GameController.instance != null){
76 GameController.instance.score = 0;
77 prevLevel = GameController.instance.currentLevel;
78 highscore.transform.GetChild (0).transform.GetComponent
79
80 if(GameController.instance.highscore[GameController.instance.currentLevel - 1] > 0){
81 highscore.gameObject.SetActive (true);
82 }
83
84 }
85
86 }
File name: GameplayController.cs
Copy
198 void DistanceBetweenCannonAndBullet(){
199 GameObject[] bullet = GameObject.FindGameObjectsWithTag ("Player Bullet");
200 foreach (GameObject distanceToBullet in bullet) {
201 if (!distanceToBullet.transform.GetComponent
202 if (distanceToBullet.transform.position.x - player.position.x > distance) {
203 camera.isFollowing = true;
204 checkGameStatus = true;
205 timeSinceStartedShot = Time.time;
206 TimeSinceShot ();
207 camera.target = distanceToBullet.transform;
208 } else {
209 if(PlayerBullet() == 0){
210 camera.isFollowing = true;
211 checkGameStatus = true;
212 timeSinceStartedShot = Time.time;
213 TimeSinceShot ();
214 camera.target = distanceToBullet.transform;
215 }
216 }
217 }
218 }
219 /*if (GameObject.FindGameObjectWithTag ("Player Bullet") != null) {
220 if (!GameObject.FindGameObjectWithTag ("Player Bullet").transform.GetComponent
221 Transform distanceToBullet = GameObject.FindGameObjectWithTag ("Player Bullet").transform;
222 if (distanceToBullet.position.x - player.position.x > distance) {
223 camera.isFollowing = true;
224 checkGameStatus = true;
225 TimeSinceShot ();
226 camera.target = distanceToBullet;
227 }
228 }
229
230 }*/
231 }
File name: GameManager.cs
Copy
12 void Start()
13 {
14 blocks = GameObject.FindGameObjectsWithTag("Block");
15 Ball = GameObject.Find("Ball").GetComponent
16 statusText = GameObject.Find("Status").GetComponent
17 }
File name: BackgroundScript.cs
Copy
11 void Start () {
12 background = GameObject.FindGameObjectsWithTag("Background");
13
14 lastBackground = background[0].transform.position.x;
15
16 for (int i = 1; i < background.Length; i++) {
17
18 if (lastBackground < background[i].transform.position.x)
19 {
20
21 lastBackground = background[i].transform.position.x;
22 }
23 }
24 }
FindGameObjectsWithTag 242 lượt xem
Gõ tìm kiếm nhanh...