Project 1: Space Shooter

How to access another script’s methods in Unity

By introducing health and damage to the Player object

Lives, damage and collisions working with the enemies.

Thus far my player has been immortal, but today I will remedy that by giving some health to my player. This is done by simply adding a new private integer type variable called _lives to my player and setting it to some value, three lives seems quite common so three it is.

The method of dealing damage is as simple as the amount of lives is. All I have to do is to make a public method type void called Damage and reduce the _lives by one every time the method is called, and an if-statement to check if the _lives has reached zero so that the Player object can be destroyed.

Remember to type the keyword public before the type and name of the method, because by default all the methods inside a script are private and cannot thus be accessed from other scripts.

The Player script’s Damage method.

So how to get my Enemy object’s script to access this Damage method? Since the argument of the OnTriggerEnter gives access to the colliding object, named other by default, I can access the Damage method inside the Player script by finding the Transform component and using a method called GetComponent to find the Player script and then access the Damage method.

Accessing the Player script’s Damage method inside the OnTriggerEnter method in the Enemy class.

Now, if for some reason the Player object does not have a script called Player, the GetComponent method will return null as its value. Unless it is checked if the GetComponent returned what I wanted, will Unity produce an error and in the worst case cause the game to crash. By doing these checks the game, or a program in general, keeps on running, it just misses some features.

Error message when the GetComponent’s return value is not checked against the possible null value.




Unity game developer in the making.

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

Recommended from Medium

ETACUDE Free Data Analysis Free Training

Your first game are gonna suck | Wannabe Indie Gamedev #23

New Structure, New Roles — TiDB Community Upgrade!

An Introduction to AWS PowerShell Tools in CloudShell

Introduction to Python Programming: Sequence — List, Tuples, and Range

Why property Wrappers are so important in Swift?

Start Learning python with Exercices

Asyncio Is Not Parallelism. You may have heard that Python asyncio… | by  Peter Xie | Towards Data Science

🎉 Your Pockit account is active again!🎉

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
Eero Saarinen

Eero Saarinen

Unity game developer in the making.

More from Medium

Introduction to Unity Coroutines

How to Merge Unity Project Updates to Github Repository

Guide to Making a Beautiful Game: Light Cookies in HDRP

The Difference Between OnTriggerEnter and OnCollisionEnter in Unity