Static Types Pt. 2- C# Skill in Unity: 3

Objective: using static type to create utility helper class.

Photo by Halacious on Unsplash

Static type can also be very helpful by using it to create a utility helper. As static type can be called from any class, it can hold some methods to be called during the game.

Position adjuster

Let’s create a helper that would readjust the position of a gameobject.
Create a new script and set this script as a static class.

Then create a method with passing in a gameobject, and readjust the position of this gameobject to a fixed position.

As this method, you can imagine that sometimes you might need to set something back to a fixed position during the game and you have not set the reference, or it can be a reset system when you want to readjust it.

Now our method is ready, let’s call it from another script.

Using utility helper

Create another script and assign it to an object in game.

Then we can call the ReadjustPosition() method in Player script.

Now we can test it in the Play mode.

As you can see it is so easy to call a method from a static class.
And that is how to create a utility helper by using static class.

--

--

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