FindGameObjectWithTag









How do I use Find Game Object With Tag
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: GameManager.cs Copy
15  void Start () {
16   theBall = GameObject.FindGameObjectWithTag ("Ball");
17  }
File name: BossHealth.cs Copy
44  void BossDestroyed(){
45   Destroy (gameObject);
46   GameObject.FindGameObjectWithTag ("Spawner").transform.GetComponent ().active = true;
47   GameObject.FindGameObjectWithTag ("Spawner").transform.GetComponent ().isBossReady = false;
48   if(GameController.instance != null && MusicController.instance != null){
49    if(GameController.instance.isMusicOn){
50     MusicController.instance.audioSource.PlayOneShot (MusicController.instance.bossExplode);
51    }
52   }
53   Instantiate (explode, transform.position, Quaternion.identity);
54   for (int i = 0; i < 5; i++) {
55    Instantiate (coin, transform.position, Quaternion.identity);
56   }
57  }
File name: BossShoot.cs Copy
19  void Update () {
20   if(isReadyToShoot){
21    fireRate = Random.Range(4, 6);
22    firstDelay += Time.deltaTime;
23    if (firstDelay >= fireRate) {
24     if (!fireBullet) {
25      fireBullet = true;
26      if (GameObject.FindGameObjectWithTag ("Player") != null) {
27       GameObject newBullet = Instantiate (bullet, new Vector3 (transform.position.x, 0.3f, transform.position.z), Quaternion.identity) as GameObject;
28       Transform target = GameObject.FindGameObjectWithTag ("Player").transform;
29       newBullet.GetComponent ().velocity = (target.position - transform.position).normalized * 5f;
30      } else {
31       GameObject newBullet = Instantiate (bullet, new Vector3 (transform.position.x, 0.3f, transform.position.z), Quaternion.identity) as GameObject;
32       newBullet.GetComponent ().velocity = new Vector2 (Random.Range(-1f, 1f), -2f);
33      }
34     }
35
36     if (fireBullet) {
37      secondDelay += Time.deltaTime;
38      if (secondDelay >= 0.5f) {
39       secondDelay = 0;
40       firstDelay = 0;
41       if (GameObject.FindGameObjectWithTag ("Player") != null) {
42        GameObject newBullet = Instantiate (bullet, new Vector3 (transform.position.x, 0.3f, transform.position.z), Quaternion.identity) as GameObject;
43        Transform target = GameObject.FindGameObjectWithTag ("Player").transform;
44        newBullet.GetComponent ().velocity = (target.position - transform.position).normalized * 5f;
45       } else {
46        GameObject newBullet = Instantiate (bullet, new Vector3 (transform.position.x, 0.3f, transform.position.z), Quaternion.identity) as GameObject;
47        newBullet.GetComponent ().velocity = new Vector2 (Random.Range(-0.1f, 0.1f), -2f);
48       }
49       fireBullet = false;
50      }
51     }
52    }
53
54   }
55
56
57  }
File name: EnemySpawner.cs Copy
82  void CheckedEnemies(){
83   if(!active){
84    if (GameObject.FindGameObjectWithTag("Enemy") == null) {
85     if(!isBossReady){
86      isBossReady = true;
87      GameplayController.instance.ShowWarning();
88     }
89    }
90   }
91  }
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
159  IEnumerator SpawnBossTime(){
160   warningText.gameObject.SetActive (true);
161   warningText.transform.GetComponent ().PlayWarning ();
162   yield return new WaitForSeconds (5f);
163   warningText.gameObject.SetActive (false);
164   warningText.transform.GetComponent ().StopWarning ();
165   GameObject.FindGameObjectWithTag ("Spawner").transform.GetComponent ().SpawnBoss ();
166  }
File name: GameplayController.cs Copy
221  public void RetryButton(){
222   InitializeGameplayVariables ();
223   ClearAllEnemies ();
224   gameInProgress = false;
225   GameObject.FindGameObjectWithTag ("Spawner").transform.GetComponent ().active = false;
226  }
File name: GameplayController.cs Copy
228  public void InitializeSpawners(){
229   GameObject.FindGameObjectWithTag ("Spawner").transform.GetComponent ().InitializeVariables ();
230   gameInProgress = true;
231  }
File name: Enemy.cs Copy
188     IEnumerator ShootingRoutine_03()
189     {
190         GameObject tempPlayer = GameObject.FindGameObjectWithTag("Player");
191
192         //if player didnt destroyed
193         if (tempPlayer != null)
194             player = tempPlayer.GetComponent();
195         else
196             player = transform;
197
198         GameObject bullet = null;
199         int bulletSpeed = 9;
200
201         while (true)
202         {
203             yield return new WaitForSeconds(1.5f);
204
205             bullet = pools.GetPoolableObject("e_bullet");
206
207             if (bullet != null)
208             {
209                 bullet.SetActive(true);
210                 bullet.GetComponent().ShootMe(transform.position, (player.position - transform.position).normalized, bulletSpeed);
211             }
212         }
213     }
File name: CameraFollow.cs Copy
30  void Update () {
31   if (GameplayController.instance.gameInProgress) {
32    if (isFollowing) {
33     if (GameObject.FindGameObjectWithTag ("Player Bullet") != null) {
34      MoveCameraFollow ();
35     }
36    } else {
37     if (!GameplayController.instance.player.GetChild (0).transform.GetComponent ().readyToShoot) {
38      MoveCameraBackToStart ();
39      AfterShotMoveAgain ();
40      allowToMove = false;
41     } else {
42      timeSinceShot = 0;
43      allowToMove = true;
44     }
45
46    }
47
48    if (Application.platform == RuntimePlatform.Android) {
49     TouchMoveCamera ();
50    } else if (Application.platform == RuntimePlatform.WindowsEditor) {
51     MoveCamera ();
52    }
53   }
54
55  }

FindGameObjectWithTag 188 lượt xem

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