Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning C# by Developing Games with Unity 2020 - Fifth Edition

You're reading from  Learning C# by Developing Games with Unity 2020 - Fifth Edition

Product type Book
Published in Aug 2020
Publisher Packt
ISBN-13 9781800207806
Pages 366 pages
Edition 5th Edition
Languages
Author (1):
Harrison Ferrone Harrison Ferrone
Profile icon Harrison Ferrone

Table of Contents (16) Chapters

Preface Getting to Know Your Environment The Building Blocks of Programming Diving into Variables, Types, and Methods Control Flow and Collection Types Working with Classes, Structs, and OOP Getting Your Hands Dirty with Unity Movement, Camera Controls, and Collisions Scripting Game Mechanics Basic AI and Enemy Behavior Revisiting Types, Methods, and Classes Introducing Stacks, Queues, and HashSets Exploring Generics, Delegates, and Beyond The Journey Continues Pop Quiz Answers Other Books You May Enjoy

Time for action – calling a base constructor

Since we want all Paladin objects to have a name, and Character already has a constructor that handles this, we can call the base constructor directly from the Paladin class and save ourselves the trouble of rewriting a constructor:

  1. Add a constructor to the Paladin class that takes in a string parameter, called name:
    • Use a colon and the base keyword to call the parent constructor, passing in name:
      public class Paladin: Character
{
public Paladin(string name): base(name)
{

}
}
  1. Create a new Paladin instance, called knight, in LearningCurve: 
    • Use the base constructor to assign a value.
    • Call PrintStatsInfo from knight and take a look at the console:
      Paladin knight = new Paladin("Sir Arthur");
knight.PrintStatsInfo();

The debug log will be the same as our other Character instances, but with the name that we assigned to the Paladin constructor...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}