Script Communication in Unity using GetComponent

George Asiedu
2 min readSep 18, 2022

--

Script communication is a function I have taken time to understand within unity. Using gameobjects within unity to make a game everything is done through scripts and to make our ideas come to life these objects need to interact with each other.

game scripts

example here of our scripts. we haev an enemy, laser and player script adn at some point the will have to talk to each other. The player shoots the laser which destroys the enemy but how do they know each gameobject is aware of each other.

by storing a link within our script we can call gameobjects components within the script being worked on.

here we store the player script within a variable

if the player is not equal to the gameobject which is the enemy run this method which is being called from the player script

this piece of code is within the player script and is run once the enemy and player collide

--

--

No responses yet