Script Communication in Unity using GetComponent

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.

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store