Simon Truong
2 min readMay 9, 2021

--

Scripts talking to other scripts

There will be instances when you have to reference methods from one script to another script. For example, having the enemy script communicate with the player script to check when a player loses a life upon collision.

Getting Components

By default, the only component you have direct access to is the “transform” component. To grant access to other components, you would need the script “GetComponent.”

An enemy script getting access to the player script using Get Component

Once you have access to the desired component, you can retrieve any component within the object, in this case we wanted the damage method.

An example of getting the Damage method from the player component by adding “ .Damage();”

Null Checking

Null checking is a crucial method of double checking for mistakes. Especially when trying to grant access to components that might not exist or maybe its a bad link. In worst cases scenario, having a non existent component might lead to errors or crashing of the application.

To prevent that from happening, we can create a variable that contains the component, then have an if statement to null check if component is indeed active.

Now when the script check the component and it doesn’t equal to null, it will execute the player damage method.

TLDr: Null checking is important and every developer should get into a proper habit in doing it, because it prevents fatal errors.

--

--

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.