Now they talk!?!

At this point in our space shooter we have enemies, a player and lasers. We want to have the player damage itself when the enemy hits it. We’re checking the collision in OnTriggerEnter on the enemy component, how can we let the player know to take damage?

This is achieved through script communication. In this simple version of the game that communication is accomplished through the use of a public method exposed on the Player class, so that the enemy class can see it, and the GetComponent() function.

GetComponent() allows us to find a component of type T if the gameObject we are checking has one attached. It returns null if the component doesn’t exist on the gameObject. In our case, we are using GetComponent() to try to retrieve the Player script component from the object that collided with our enemy, which in our use case should be the player since we have checked the tag on the object to make sure that it is the player. We then check the result to make sure that it is not null, meaning the gameObject that collided with our enemy does have a Player script component attached to it.

Once we have a reference to our Player script component we can then call the public Damage function that will have our Player execute the code that occurs when it takes damage.

Next Time!

Now we have the ability for our enemy script to communicate with our player script! There are better ways to do this but in our simple game this works for now. In future articles we may look at more advanced ways to accomplish this goal. Tomorrow we will look at coroutines with Unity. If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to import code base on platform in flutter?

Hacking Scikit-Learn’s Vectorizers

One Development Team, Unlimited Deployments with Heroku

HEARTBIT  DELIVERING A CALIBER OF HEALTH DATA PREVIOUSLY UNACHIEVABLE THROUGH BLOCKCHAIN AND…

Fixing the SimpleScreenRecorder black screen error on Debian Buster

Upgrading & Scaling Airflow at Robinhood

Flutter plugin story (Part 6): chain of filters

Implementing a Cache Invalidation in Distributed Cache Engine: LRU [With Code Implementation]

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
Christopher West

Christopher West

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.

More from Medium

Connecting GitHub to Unity

In 6 steps, Add a double jump in Unity

Modifying existing game object textures

Cinematography in Unity — Skyboxes