From the course: Learning Unity 2D Scripting

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Custom classes

Custom classes - Unity Tutorial

From the course: Learning Unity 2D Scripting

Start my 1-month free trial

Custom classes

- [Narrator] At this point, all of the scripts that we've created extend MonoBehaviour. But the beauty of working with Unity is that it actually uses .NET. And this means that we can actually write straight C# classes. The only limitation is that if we want to attach a script onto a game object, it needs to extend MonoBehaviour. But that doesn't limit us from creating additional logic in separate, pure C# classes, and using them inside of our scripts when we need to. A perfect example of this is to build a utility class to help us perform calculations. Let's go ahead and right click in the scripts folder and create a new C# script. Here, we're going to create a position utility. We'll type out P - o - s - U - t - I - l for position utility. Next, let's open this up in MonoDevelop. Now, since this is going to be our own class, let's go ahead and delete MonoBehaviour. In fact, we can also delete the "Start" and "Update" methods, since we won't be using them, and let's remove all of the…

Contents