Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Mastering Unity Scripting
  • Table Of Contents Toc
  • Feedback & Rating feedback
Mastering Unity Scripting

Mastering Unity Scripting

By : Alan Thorn
4.5 (14)
close
close
Mastering Unity Scripting

Mastering Unity Scripting

4.5 (14)
By: Alan Thorn

Overview of this book

Mastering Unity Scripting is an advanced book intended for students, educators, and professionals familiar with the Unity basics as well as the basics of scripting. Whether you've been using Unity for a short time or are an experienced user, this book has something important and valuable to offer to help you improve your game development workflow.
Table of Contents (12 chapters)
close
close
11
Index

Classes and inheritance

Imagine a scenario where you create an Orc class to encode an orc object in the game. Having done so, you then decide to make two upgraded types. One is an Orc Warlord, with better armor and weapons, and the other is an Orc Mage who, as the name implies, is a spell caster. Both can do everything that the ordinary orc can do, but more besides. Now, to implement this, you can create three separate classes, Orc, OrcWarlord, and OrcMage, by copying and pasting common code between them.

The problem is that as Orc Warlord and Orc Mage share a lot of common ground and behaviors with orc, a lot of code will be wastefully copied and pasted to replicate the common behaviors. Furthermore, if you discovered a bug in the shared code of one class, you'd need to copy and paste the fix to the other classes to propagate it. This is both tedious and technically dangerous, as it risks wasting time, introducing bugs, and causing needless confusion. Instead, the object-oriented concept of inheritance can help us. Inheritance allows you to create a completely new class that implicitly absorbs or contains the functionality of another class, that is, it allows you to build a new class that extends an existing class without affecting the original one. When inheritance happens, two classes are brought into a relationship with each other. The original class (such as the Orc class) is known as the case class or ancestor class. The new class (such as the Orc Warlord or Orc Mage), which extends on the ancestor class, is called a super class or derived class.

Tip

More information on inheritance in C# can be found at http://msdn.microsoft.com/en-gb/library/ms173149%28v=vs.80%29.aspx.

By default, every new Unity script file creates a new class derived from MonoBehaviour. This means every new script contains all the MonoBehaviour functionality and has the potential to go beyond, based on the additional code that you add. To prove this, refer to the following code sample 1-11:

01 using UnityEngine;
02 using System.Collections;
03 
04 public class NewScript : MonoBehaviour 
05 {
06 //--------------------------------------------------
07    // Use this for initialization
08    void Start ()
09    {
10       name = "NewObject";
11 }
12    //--------------------------------------------------
13    // Update is called once per frame
14    void Update ()
15    {
16    }
17 }

The following are the comments for code sample 1-11:

  • Line 04: The class NewScript is derived from MonoBehaviour. You can, however, substitute MonoBehaviour for almost any valid class name from which you want to derive.
  • Line 10: Here, the variable name is assigned a string during the Start event. However, notice that the name is not explicitly declared as a variable anywhere in the NewScript source file. If NewScript were a completely new class with no ancestor defined in line 04, then line 10 would be invalid. However, because NewScript derives from MonoBehaviour, it automatically inherits all of its variables, allowing us to access and edit them from NewScript.

    Note

    When to inherit

    Only use inheritance where it's really appropriate; otherwise, you'll make your classes large, heavy, and confusing. If you're creating a class that shares a lot of common functionality with another and it makes sense to establish connection between them, then use inheritance. Another use of inheritance, as we'll see next, is when you want to override specific functions.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Mastering Unity Scripting
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon