FindObjectOfType
How do I use Find Object Of Type
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: Singleton.cs
Copy
12 public static T Instance {
13 get {
14 T foundObject = FindObjectOfType
15
16 if (instance == null) {
17 instance = foundObject;
18 } else if (instance != foundObject) {
19 Destroy(foundObject);
20 }
21
22 if (!_destroyOnLoad) DontDestroyOnLoad(foundObject);
23 return instance;
24 }
25 }
File name: CameraScript.cs
Copy
13 void Start () {
14 player = FindObjectOfType
15 lastPlayerPosition = player.transform.position;
16 }
File name: GenerateGrounds.cs
Copy
32 void Start () {
33
34 groundWidth = new float[theObjectPools.Length];
35
36 for(int i = 0; i < theObjectPools.Length; i++) {
37 groundWidth [i] = theObjectPools [i].pooledObject.GetComponent
38 }
39
40 minHeight = transform.position.y;
41 maxHeight = maxHeightPoint.position.y;
42
43 coinGenerator = FindObjectOfType
44 cratesGenerator = FindObjectOfType
45 }
File name: PlayerMoveScript.cs
Copy
98 void OnCollisionEnter2D(Collision2D target) {
99 if(target.gameObject.tag == "died" || target.gameObject.tag == "Crates") {
100 jumpForce = 0;
101 scoreCount = 0;
102 anim.SetTrigger ("Died");
103 scoreText.gameObject.SetActive (false);
104 audioSource.PlayOneShot (diedClip);
105 FindObjectOfType
106 FindObjectOfType
107 FindObjectOfType
108 }
109 }
File name: PlayerMoveScript.cs
Copy
111 void OnTriggerEnter2D(Collider2D coin) {
112 if(coin.tag == "Coin") {
113 audioSource.PlayOneShot (coinClip);
114 coinScore++;
115 FindObjectOfType
116 coin.gameObject.SetActive (false);
117 }
118 }
File name: Enemy.cs
Copy
22 void Awake ()
23 {
24 pools = GameObject.FindObjectOfType
25 rb = GetComponent
26 sounds = GameObject.FindObjectOfType
27 uiForScore = GameObject.FindObjectOfType
28 }
File name: SpawnManager.cs
Copy
11 void Awake()
12 {
13 pools = GameObject.FindObjectOfType
14 rand = new Random();
15 }
File name: SpawnManager.cs
Copy
42 IEnumerator SpawningRoutine()
43 {
44 yield return new WaitForSeconds(3);
45
46 while (!UIgame.scoreAchived)
47 {
48 GameObject enemy = null;
49
50 //FIRST STAGE OF SPAWNING
51 //CHOOSE ENEMY FOR SPAWN
52 int r = rand.Randomization_1(0, 10, 80, 99);
53 if (r == 0)
54 {
55 enemy = pools.GetPoolableObject("enemy_01");
56 }
57 else if (r == 1)
58 {
59 enemy = pools.GetPoolableObject("enemy_02");
60 }
61 else
62 {
63 enemy = pools.GetPoolableObject("enemy_03");
64 }
65
66 //SECOND STAGE OF SPAWNING
67 //SPAWN CHOSEN ENEMY IN RANDOM POINT ON TOP SCREEN SIDE
68 r = Random.Range(0, pointsPositions.Length);
69
70 if (enemy != null)
71 {
72 enemy.SetActive(true);
73 enemy.GetComponent
74 new Vector3(0,0,-1));
75 }
76
77 int spawnDelay;
78 if (SystemScr.difficultyIsHard)
79 spawnDelay = 1;
80 else
81 spawnDelay = Random.Range(3,5);
82
83 yield return new WaitForSeconds(spawnDelay);
84 }
85
86 //BOSS ACTIVATION
87 pools.DeleteEnemiesAndBullets();
88 CameraMotor.speedScreen = 0;
89 Camera.main.GetComponent
90 Camera.main.GetComponent
91 Camera.main.backgroundColor = Color.black;
92 GameObject.FindObjectOfType
93
94 yield return new WaitForSeconds(6);
95
96 pools.AcivateBoss();
97 }
File name: Borders.cs
Copy
8 void Awake ()
9 {
10 c_motorPos = GameObject.FindObjectOfType
11 }
FindObjectOfType 146 lượt xem
Gõ tìm kiếm nhanh...