Script Communication in Unity using GetComponent<T>
As we getting deeper and deeper in our use case of Player, Lasers and Enemies - for Our GalaxyShooter -2D.Unity API Scripting methods — GetComponent<T> plays a vital role here to communicate between these GameObjects.
Take an example, how one can destroyed the Player when Enemy collide with it — both of these Game Objects having scripts.
Note : Consider here Player Script as below having Damage Method.This method reduces the Player _lives by one which is initially set to 3 and when _lives<1 then Player get destroyed
When Enemy collide with Player/Laser , as per below script -Its having onTriggerEnter event called containing other of type Collider as parameter— where other can use to find who collided with Enemy.
In our case say Enemy collided with Player. Other.tag ==”Player ” condition sets true . Then Player reference (communication of Player from enemy) can be found from GetComponent<T> where T:Player. If Player ref is found and if it’s not null- call the Damage method of the Player.
Player can be destroyed further as each time collided with the enemy reduces the live of Player by one and finally reduces to 0 as shown below..
This concludes Script Communication using GetComponent<T>where T:GameObject
@GameDevHQ-Day 10…