0% found this document useful (0 votes)
129 views

Learning C# programming with Unity 3D Alex Okita 2024 scribd download

Okita

Uploaded by

dudierbohlok
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

Learning C# programming with Unity 3D Alex Okita 2024 scribd download

Okita

Uploaded by

dudierbohlok
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Experience Seamless Full Ebook Downloads for Every Genre at textbookfull.

com

Learning C# programming with Unity 3D Alex Okita

https://textbookfull.com/product/learning-c-programming-
with-unity-3d-alex-okita/

OR CLICK BUTTON

DOWNLOAD NOW

Explore and download more ebook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

C# Game Programming Cookbook for Unity 3D 2nd Edition Jeff


W. Murray

https://textbookfull.com/product/c-game-programming-cookbook-for-
unity-3d-2nd-edition-jeff-w-murray/

textboxfull.com

Learning C 7 By Developing Games with Unity 2017 Learn C


Programming by building fun and interactive games with
Unity Third Edition Micael Dagraca
https://textbookfull.com/product/learning-c-7-by-developing-games-
with-unity-2017-learn-c-programming-by-building-fun-and-interactive-
games-with-unity-third-edition-micael-dagraca/
textboxfull.com

Learning C by developing games with Unity 2020 An


enjoyable and intuitive approach to getting started with C
programming and Unity Fifth Edition Harrison Ferrone
https://textbookfull.com/product/learning-c-by-developing-games-with-
unity-2020-an-enjoyable-and-intuitive-approach-to-getting-started-
with-c-programming-and-unity-fifth-edition-harrison-ferrone/
textboxfull.com

Beginning Game AI with Unity: Programming Artificial


Intelligence with C# Sebastiano M. Cossu

https://textbookfull.com/product/beginning-game-ai-with-unity-
programming-artificial-intelligence-with-c-sebastiano-m-cossu/

textboxfull.com
Developing 2D Games with Unity: Independent Game
Programming with C# 1st Edition Jared Halpern

https://textbookfull.com/product/developing-2d-games-with-unity-
independent-game-programming-with-c-1st-edition-jared-halpern/

textboxfull.com

Developing 2D Games with Unity: Independent Game


Programming with C# 1st Edition Jared Halpern

https://textbookfull.com/product/developing-2d-games-with-unity-
independent-game-programming-with-c-1st-edition-jared-halpern-2/

textboxfull.com

Game Programming in C Creating 3D Games Creating 3D Games


Sanjay Madhav

https://textbookfull.com/product/game-programming-in-c-
creating-3d-games-creating-3d-games-sanjay-madhav/

textboxfull.com

Getting Started with Unity 2018 A Beginner s Guide to 2D


and 3D Game Development with Unity 3rd Edition Edward
Lavieri
https://textbookfull.com/product/getting-started-with-
unity-2018-a-beginner-s-guide-to-2d-and-3d-game-development-with-
unity-3rd-edition-edward-lavieri/
textboxfull.com

Deep Reinforcement Learning in Unity: With Unity ML


Toolkit 1st Edition Abhilash Majumder

https://textbookfull.com/product/deep-reinforcement-learning-in-unity-
with-unity-ml-toolkit-1st-edition-abhilash-majumder/

textboxfull.com
Learning C# Programming
with Unity 3D
Learning C# Programming
with Unity 3D
Second Edition

Alex Okita
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742

© 2020 by Taylor & Francis Group, LLC


CRC Press is an imprint of Taylor & Francis Group, an Informa business

No claim to original U.S. Government works

Printed on acid-free paper

International Standard Book Number-13: 978-1-138-33681-0 (paperback)


978-1-138-33682-7 (hardback)

This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made
to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all
materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all
material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been
obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future
reprint.

Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized
in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying,
microfilming, and recording, or in any information storage or retrieval system, without written permission from the
publishers.

For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.
copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400.
CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been
granted a photocopy license by the CCC, a separate system of payment has been arranged.

Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for
identification and explanation without intent to infringe.

Library of Congress Cataloging‑in‑Publication Data

Names: Okita, Alex, author.


Title: Learning C# programming with Unity 3D / Alex Okita.
Description: Second edition. | Boca Raton, FL : CRC Press/Taylor & Francis
Group, 2019.
Identifiers: LCCN 2019010020 | ISBN 9781138336810 (pbk. : acid-free paper) |
ISBN 9781138336827 (hardback : acid-free paper)
Subjects: LCSH: Computer games--Programming. | C# (Computer program language)
| Unity (Electronic resource) | Three-dimensional display systems.
Classification: LCC QA76.76.C672 O43 2019 | DDC 794.8/1526--dc23
LC record available at https://lccn.loc.gov/2019010020

Visit the Taylor & Francis Web site at


http://www.taylorandfrancis.com

and the CRC Press Web site at


http://www.crcpress.com
Contents

1. Introduction: What This Book Is About ........................................................................................ 1


1.1 Why Read a Book: Why This Book May or May Not Be for You ......................................... 1
1.1.1 Do I Need to Know Math? ........................................................................................ 2
1.1.2 Programming as a Form of Expression .................................................................... 2
1.1.3 Games as a Stage with Lights ................................................................................... 2
1.2 Personal Information .............................................................................................................. 2
1.3 A Brief History of Computer Programming: How Programming Came to Be ..................... 3
1.3.1 Mechanical Computers ............................................................................................. 3
1.3.2 Logic ......................................................................................................................... 3
1.3.3 Computer Science ..................................................................................................... 3
1.3.4 The Origin of Software ............................................................................................. 4
1.3.5 Modern Computer Language .................................................................................... 4
1.3.6 The Future of Computer Languages ......................................................................... 4
1.4 C#: A Flexible Programming Language ................................................................................ 4
1.4.1 C# Paradigm ............................................................................................................. 5
1.4.2 Unity 3D: A Brief History of Game Engines ........................................................... 5
1.4.3 Why Use Unity 3D to Learn? ................................................................................... 6
1.4.4 How Does Unity 3D Use C#? ................................................................................... 6
1.4.4.1 How to Tell Unity 3D What to Do............................................................ 7
1.5 What Is Programming? ........................................................................................................... 7
1.5.1 What Does C# Look Like? ....................................................................................... 8
1.5.2 Learning to Copy and Paste ...................................................................................... 9
1.5.3 Iteration ..................................................................................................................... 9
1.6 Compiling: Turning Words into Computer Instruction .......................................................... 9
1.7 What We’ve Learned ............................................................................................................ 10
1.8 Leveling Up: The Path Ahead .............................................................................................. 10

2. Before You Begin .............................................................................................................................11


2.1 What Will Be Covered in This Chapter ................................................................................11
2.2 Unity 3D Installation ............................................................................................................ 13
2.2.1 Visual Studio Installation ....................................................................................... 15
2.2.2 Unity Version Numbering ........................................................................................16
2.3 Git SCM Installation ..............................................................................................................17
2.3.1 Git-SCM.com Git Software (Optional) ....................................................................17
2.3.2 GitHub.com Sign Up! (Also Optional) ....................................................................18
2.3.3 Git: The Basics ........................................................................................................ 20
2.3.4 Your First Repo ....................................................................................................... 22
2.3.5 Before Moving On .................................................................................................. 25
2.4 Unity General Overview ....................................................................................................... 26
2.4.1 The Main Window .................................................................................................. 27
2.4.1.1 Toolbar .................................................................................................... 27
2.4.1.2 Scene, Game View, and Asset Store....................................................... 28
2.4.1.3 Hierarchy Panel ...................................................................................... 28

v
vi Contents

2.4.1.4 Inspector Panel ....................................................................................... 29


2.4.1.5 Project and Console Panel ...................................................................... 29
2.4.1.6 Assets Directory ......................................................................................31
2.4.1.7 Project Settings Directory ...................................................................... 32
2.4.1.8 Packages Directory ................................................................................. 32
2.4.2 Git Ignore ................................................................................................................ 32
2.4.3 What We’ve Learned .............................................................................................. 32
2.5 Sample Code ......................................................................................................................... 33
2.5.1 Code Fragments ...................................................................................................... 33
2.5.2 Are Errors Bad? ...................................................................................................... 34
2.5.2.1 Compile-Time and Run-Time Errors...................................................... 34
2.5.3 How to Follow Along .............................................................................................. 35
2.5.4 Comments: A First Look ........................................................................................ 36
2.5.5 What We’ve Learned .............................................................................................. 37
2.6 Working with C#: Game Engines ......................................................................................... 37
2.6.1 Creating and Assigning a New C# File: Now It’s Your Turn ................................. 40
2.6.2 Naming Your New File ........................................................................................... 42
2.6.3 Using Your New File............................................................................................... 43
2.6.4 Additional Unity 3D Tools ...................................................................................... 48
2.6.5 Working in a Live Scene ......................................................................................... 50
2.6.6 Saving a Scene ........................................................................................................ 50
2.6.7 Opening a Scene ..................................................................................................... 50
2.6.8 What We’ve Learned ...............................................................................................51
2.7 Leveling Up: The Journey Has Just Begun............................................................................51

3. First Steps: Just Getting Started .................................................................................................. 53


3.1 What Will Be Covered in This Chapter ............................................................................... 53
3.2 Review................................................................................................................................... 54
3.2.1 Submitting Changes to Git ...................................................................................... 54
3.3 Let’s Build a Game—Part 1 ................................................................................................. 56
3.3.1 Movement Experiments .......................................................................................... 57
3.3.2 Reading Programmer Documentation .................................................................... 58
3.4 Tokens ....................................................................................................................................61
3.4.1 Writing C#............................................................................................................... 62
3.4.2 Comments a First Look........................................................................................... 63
3.4.3 Separator Tokens ..................................................................................................... 64
3.4.4 Operator Tokens ...................................................................................................... 65
3.4.5 Literals .................................................................................................................... 66
3.4.6 Transitive and Non-Transitive Operations .............................................................. 67
3.4.7 Putting It All Together ............................................................................................ 67
3.4.8 What We’ve Learned .............................................................................................. 69
3.5 Statements and Expressions .................................................................................................. 69
3.5.1 Expressions ............................................................................................................. 70
3.5.2 How Unity 3D Executes Your Code ....................................................................... 70
3.5.3 Thinking in Algorithms .......................................................................................... 70
3.5.3.1 Wash, Rinse, Repeat ............................................................................... 73
3.5.4 What We’ve Learned .............................................................................................. 73
3.6 Keywords .............................................................................................................................. 73
3.6.1 The Class Keyword ................................................................................................. 74
3.6.2 What We’ve Learned .............................................................................................. 76
3.7 White Space .......................................................................................................................... 76
3.7.1 Code Style ............................................................................................................... 78
3.7.2 What We’ve Learned .............................................................................................. 79
Contents vii

3.8 Code Blocks ........................................................................................................................ 79


3.8.1 What We’ve Learned .......................................................................................... 81
3.9 Classes: A First Look.......................................................................................................... 81
3.9.1 Objects ................................................................................................................ 82
3.9.2 What We’ve Learned .......................................................................................... 86
3.10 Variables ............................................................................................................................. 86
3.10.1 Identifiers ............................................................................................................ 87
3.10.2 Data ..................................................................................................................... 88
3.10.3 Variable Manipulation ........................................................................................ 89
3.10.3.1 Declaration Placement Examples ..................................................... 90
3.10.4 Dynamic Initialization ........................................................................................ 90
3.10.5 What We’ve Learned .......................................................................................... 91
3.11 Variable Names ................................................................................................................... 91
3.11.1 Useful Variables in Unity 3D .............................................................................. 94
3.11.2 Variable Assignment ........................................................................................... 96
3.11.3 Putting It Together: Using the Variables............................................................. 98
3.11.4 What We’ve Learned ........................................................................................ 100
3.12 Types: A First Look .......................................................................................................... 100
3.12.1 Value and Reference Types ............................................................................... 100
3.12.2 What We’ve Learned .........................................................................................101
3.13 Strong Typing.................................................................................................................... 102
3.13.1 Dynamic Typing ............................................................................................... 102
3.13.2 What We’ve Learned ........................................................................................ 103
3.14 Type Casting, Numbers .................................................................................................... 103
3.14.1 Explicit versus Implicit Casting ........................................................................ 104
3.14.1.1 A Basic Example ............................................................................. 105
3.14.2 What We’ve Learned ........................................................................................ 107
3.15 Comments ......................................................................................................................... 108
3.15.1 Line Numbers.....................................................................................................111
3.15.2 Code Folding ......................................................................................................111
3.15.3 Summary Comments .........................................................................................113
3.15.4 Navigating in Code ............................................................................................114
3.15.5 What We’ve Learned .........................................................................................115
3.16 Leveling Up: Moving on to Basics ....................................................................................115
3.16.1 Git Revision History ..........................................................................................115

4. Basics: The Building Blocks of Code ...........................................................................................117


4.1 What Will Be Covered in This Chapter ............................................................................117
4.2 Review ...............................................................................................................................117
4.3 Building a Game—Part 1 ..................................................................................................118
4.3.1 Mouse Point .......................................................................................................119
4.3.2 Move to Point .................................................................................................... 120
4.3.3 What We’ve Learned .........................................................................................121
4.4 Class Construction .............................................................................................................121
4.4.1 Class Declaration .............................................................................................. 123
4.4.1.1 A Basic Example ............................................................................. 123
4.4.1.2 Value and Reference Types ............................................................. 124
4.4.2 Adding Data Fields ........................................................................................... 125
4.4.3 Access Modifiers and the Dot Operator ........................................................... 125
4.4.4 Class Scope ....................................................................................................... 127
4.4.5 Class Members .................................................................................................. 128
4.4.6 What We’ve Learned ........................................................................................ 128
viii Contents

4.5 Directives .......................................................................................................................... 129


4.5.1 Libraries.......................................................................................................... 130
4.5.2 Ambiguous NameSpaces .................................................................................133
4.5.3 What We’ve Learned ...................................................................................... 134
4.6 Functions........................................................................................................................... 134
4.6.1 What Are Functions? .......................................................................................135
4.6.2 Unity 3D Entry Points .................................................................................... 136
4.6.3 Writing a Function .......................................................................................... 137
4.6.4 More on White Space and Tabs ...................................................................... 139
4.6.5 What We’ve Learned ...................................................................................... 140
4.7 Order of Operation: What Is Calculated and When ......................................................... 140
4.7.1 Evaluating Numbers ........................................................................................141
4.7.1.1 Math ...............................................................................................141
4.7.1.2 Operator Evaluation...................................................................... 144
4.7.2 What We’ve Learned .......................................................................................145
4.8 Scope: A First Look .......................................................................................................... 146
4.8.1 Class Scope ..................................................................................................... 146
4.8.2 Function Scope ................................................................................................149
4.8.3 Blank Scope .....................................................................................................151
4.8.4 What We’ve Learned .......................................................................................152
4.9 This ....................................................................................................................................152
4.9.1 A Basic Example .............................................................................................152
4.9.2 When This Is Necessary..................................................................................153
4.9.3 What We’ve Learned ...................................................................................... 154
4.10 Building a Game—Part 2 ..................................................................................................155
4.10.1 Additional Components ...................................................................................155
4.10.2 The Projectile ................................................................................................. 158
4.10.3 The Target ....................................................................................................... 160
4.10.4 What We’ve Learned .......................................................................................161
4.11 Logic and Operators ..........................................................................................................162
4.11.1 Booleans ..........................................................................................................162
4.11.2 Equality Operators ..........................................................................................163
4.11.3 Logical Not! .....................................................................................................165
4.11.4 Greater and Less than Operators .................................................................... 166
4.11.5 Logical And and Or Operators ........................................................................168
4.11.6 If and Branching ..............................................................................................169
4.11.7 What We’ve Learned .......................................................................................173
4.12 Loops .................................................................................................................................173
4.12.1 Unary Operators ..............................................................................................173
4.12.2 While ...............................................................................................................174
4.12.3 For ....................................................................................................................175
4.12.4 Do-While .........................................................................................................177
4.12.5 Postfix and Prefix Notation..............................................................................178
4.12.6 Using Loops .....................................................................................................179
4.12.7 Loops within Loops.........................................................................................182
4.12.8 Runaway Loops ...............................................................................................183
4.12.9 Breakpoints: A First Look ...............................................................................185
4.12.10 What We’ve Learned .......................................................................................186
4.13 ScopeAgain ........................................................................................................................187
4.13.1 Visibility or Accessibility ................................................................................187
4.13.1.1 A Basic Example ...........................................................................187
4.13.2 Global Scope....................................................................................................188
4.13.3 What We’ve Learned ...................................................................................... 190
Contents ix

4.14 Warnings versus Errors .................................................................................................... 190


4.14.1 Warnings ........................................................................................................... 190
4.14.2 Errors .................................................................................................................191
4.14.3 Understanding the Debugger ............................................................................ 192
4.14.4 What We’ve Learned ........................................................................................ 194
4.15 Leveling Up: Fundamentals .............................................................................................. 194
4.15.1 Style Guides ...................................................................................................... 194

5. Fundamentals: Building a Foundation ...................................................................................... 195


5.1 What Will Be Covered in This Chapter ........................................................................... 195
5.2 Review .............................................................................................................................. 196
5.2.1 Modular Code ................................................................................................... 199
5.3 Inheritance: A First Look ................................................................................................. 202
5.3.1 Class Members .................................................................................................. 204
5.3.1.1 Inheriting Members .......................................................................... 205
5.3.1.2 Is-A and Has-A ................................................................................. 206
5.3.2 Instancing .......................................................................................................... 206
5.3.3 Parent Child ...................................................................................................... 208
5.3.4 Object ................................................................................................................ 209
5.3.4.1 A Type Is Not an Object ....................................................................212
5.3.5 != null ................................................................................................................213
5.3.6 What We’ve Learned .........................................................................................214
5.4 Instancing...........................................................................................................................214
5.4.1 Class Initialization .............................................................................................215
5.4.2 New ....................................................................................................................215
5.4.3 Constructors .......................................................................................................216
5.4.4 What We’ve Learned .........................................................................................219
5.5 Static ................................................................................................................................. 220
5.5.1 A Basic Example ............................................................................................... 220
5.5.2 Static Variables ................................................................................................. 222
5.5.2.1 A Basic Example............................................................................... 222
5.5.3 Static Functions ................................................................................................. 224
5.5.4 Putting It All Together ...................................................................................... 225
5.5.5 What We’ve Learned ........................................................................................ 228
5.6 Building A Game—Part 3 ................................................................................................ 228
5.6.1 The Player ......................................................................................................... 228
5.6.2 The Mover ......................................................................................................... 229
5.6.3 The Shooter ....................................................................................................... 230
5.6.4 Physics Settings................................................................................................. 232
5.6.5 What We’ve Learned ........................................................................................ 233
5.7 Jump Statements and Return ............................................................................................ 233
5.7.1 Return................................................................................................................ 234
5.7.1.1 A Basic Example............................................................................... 235
5.7.2 Returning Objects ............................................................................................. 235
5.7.3 A Class Is a Type .............................................................................................. 236
5.7.4 Tuples ................................................................................................................ 238
5.7.5 Null Is Not Void ................................................................................................ 240
5.7.6 What We’ve Learned ........................................................................................ 240
5.8 Arrays: A First Look......................................................................................................... 241
5.8.1 Fixed-Sized Arrays ........................................................................................... 241
5.8.1.1 A Basic Example............................................................................... 241
5.8.2 Foreach .............................................................................................................. 246
5.8.2.1 A Basic Example............................................................................... 246
x Contents

5.8.3 Dynamic Initialization ...................................................................................... 246


5.8.4 Using the While Loop with Arrays................................................................... 247
5.8.4.1 Setting Array Values ....................................................................... 247
5.8.4.2 Getting Array Values ...................................................................... 248
5.8.5 What We’ve Learned ........................................................................................ 248
5.9 Jump Statements: Break and Continue ............................................................................. 248
5.9.1 A Basic Example ............................................................................................... 249
5.9.1.1 Continue .......................................................................................... 249
5.9.2 ZombieData ....................................................................................................... 249
5.9.3 Foreach Again ....................................................................................................251
5.9.4 What We’ve Learned ........................................................................................ 252
5.10 Multidimensional Arrays .................................................................................................. 252
5.10.1 Columns and Rows ........................................................................................... 253
5.10.1.1 A Basic Example ............................................................................. 254
5.10.2 A Puzzle Board ................................................................................................. 255
5.10.3 Camera and Screen Setup ................................................................................. 257
5.10.4 Checking Range ................................................................................................ 259
5.10.5 What We’ve Learned .........................................................................................261
5.11 Array List .......................................................................................................................... 262
5.11.1 A Basic Example ............................................................................................... 263
5.11.2 ArrayList.Contains() ............................................................................. 265
5.11.3 Remove.............................................................................................................. 266
5.11.4 Sort and Reverse ............................................................................................... 267
5.11.5 What We’ve Learned ........................................................................................ 268
5.12 Strings ............................................................................................................................... 268
5.12.1 Declaring a String ............................................................................................. 268
5.12.1.1 A Basic Example ............................................................................. 269
5.12.2 Escape Sequences ............................................................................................. 270
5.12.3 Verbatim Strings: @.......................................................................................... 271
5.12.4 What We’ve Learned ........................................................................................ 272
5.13 Combining What We’ve Learned ..................................................................................... 272
5.13.1 Timers ............................................................................................................... 273
5.13.2 What We’ve Learned ........................................................................................ 278
5.14 Leveling Up: On to the Cool Stuff.................................................................................... 278

6. Intermediate: The Tricks of the Trade ...................................................................................... 281


6.1 What Will Be Covered in This Chapter ........................................................................... 281
6.2 Review .............................................................................................................................. 281
6.2.1 Class Members .................................................................................................. 282
6.2.1.1 A Basic Example ............................................................................. 282
6.2.1.2 Thinking Like a Programmer ......................................................... 283
6.2.2 Return................................................................................................................ 286
6.2.2.1 A Basic Example ............................................................................. 286
6.2.3 Arguments aka “Args” (Not Related to Pirates) ............................................... 287
6.2.3.1 The Basic Example ......................................................................... 287
6.2.3.2 Multiple Args .................................................................................. 288
6.2.3.3 Using Args ...................................................................................... 290
6.2.4 Assignment Operators....................................................................................... 292
6.2.4.1 A Basic Example ............................................................................. 292
6.2.5 What We’ve Learned ........................................................................................ 294
Contents xi

6.3 Building a Game—Part 4 ................................................................................................. 294


6.3.1 ChaseCamera .................................................................................................... 294
6.3.2 Tile Floor Generator ......................................................................................... 295
6.3.3 What We’ve Learned ........................................................................................ 297
6.4 Class Constructors ............................................................................................................ 298
6.4.1 A Basic Example ............................................................................................... 299
6.4.2 What We’ve Learned ........................................................................................ 301
6.5 Arrays Revisited ............................................................................................................... 302
6.5.1 Using Arrays in Unity 3D ................................................................................. 302
6.5.1.1 Starting with 0 .................................................................................. 304
6.5.1.2 Mathf................................................................................................. 306
6.5.1.3 Time .................................................................................................. 307
6.5.2 Instancing with AddComponent(); .............................................................. 307
6.5.3 Type Casting Unity 3D Objects ........................................................................ 309
6.5.4 What We’ve Learned .........................................................................................311
6.6 Enums ................................................................................................................................311
6.6.1 Using Enums ......................................................................................................312
6.6.2 Combining What We’ve Learned ......................................................................315
6.6.3 What We’ve Learned .........................................................................................317
6.7 Switch ................................................................................................................................317
6.7.1 A Basic Example ................................................................................................318
6.7.2 Default ................................................................................................................319
6.7.3 More On Cases .................................................................................................. 322
6.7.4 Fall Through ..................................................................................................... 326
6.7.5 goto Case ........................................................................................................... 327
6.7.6 Finite State Machine ......................................................................................... 328
6.7.7 What We’ve Learned ........................................................................................ 330
6.8 Structs ............................................................................................................................... 330
6.8.1 Building a Struct ................................................................................................331
6.8.2 Struct versus Class .............................................................................................332
6.8.3 Without Structs ................................................................................................. 334
6.8.4 Handling Structs ................................................................................................335
6.8.5 Accessing Structs .............................................................................................. 336
6.8.6 Global Access ................................................................................................... 337
6.8.7 What We’ve Learned ........................................................................................ 337
6.9 Class Data ......................................................................................................................... 338
6.9.1 Character Base Class ........................................................................................ 339
6.9.2 Const ................................................................................................................. 340
6.9.3 Readonly ............................................................................................................341
6.9.4 What We’ve Learned ........................................................................................ 342
6.10 Namespaces ...................................................................................................................... 342
6.10.1 A Basic Example ............................................................................................... 342
6.10.2 Directives in Namespaces ................................................................................. 344
6.10.3 Ambiguous References ..................................................................................... 345
6.10.4 Alias Directives................................................................................................. 345
6.10.5 What We’ve Learned ........................................................................................ 346
6.11 Functions Again ................................................................................................................ 346
6.11.1 Parameter Lists ................................................................................................. 346
6.11.2 Side Effects ....................................................................................................... 347
6.11.3 Multiple Arguments .......................................................................................... 348
xii Contents

6.11.4 Useful Parameters ............................................................................................. 348


6.11.4.1 The Rule of Three ........................................................................... 349
6.11.5 Foreach versus For ............................................................................................ 350
6.11.6 What We’ve Learned .........................................................................................351
6.12 Unity 3D Execution Order .................................................................................................351
6.12.1 A Basic Example ................................................................................................353
6.12.2 Component Execution Order ............................................................................ 356
6.12.3 What We’ve Learned ........................................................................................ 358
6.13 Inheritance Again ............................................................................................................. 358
6.13.1 Function Overrides ........................................................................................... 359
6.13.1.1 A Basic Example ............................................................................. 359
6.13.2 Class Inheritance............................................................................................... 362
6.13.2.1 Sharing Common Attributes ........................................................... 362
6.13.3 Object ................................................................................................................ 364
6.13.4 What We’ve Learned ........................................................................................ 367
6.14 Type Casting Again .......................................................................................................... 368
6.14.1 (<Type>) versus “as” ......................................................................................... 368
6.14.2 User-Defined Type Conversion ......................................................................... 370
6.14.3 Implicit versus Explicit Type Conversion ......................................................... 372
6.14.4 What We’ve Learned ........................................................................................ 373
6.15 Working with Vectors ........................................................................................................374
6.15.1 Vectors Are Objects ...........................................................................................374
6.15.2 Stepping through MonsterGenerator .................................................................375
6.15.3 Gizmos .............................................................................................................. 377
6.15.3.1 A Basic Example ............................................................................. 377
6.15.3.2 DrawRay.......................................................................................... 379
6.15.4 Using Gizmos.................................................................................................... 380
6.15.5 What We’ve Learned .........................................................................................381
6.16 goto Labels .........................................................................................................................381
6.16.1 Skipping Down ................................................................................................. 383
6.16.2 Zombie State Machine ...................................................................................... 383
6.16.3 This as a Reference to Yourself ........................................................................ 386
6.16.4 Just for Zombies ................................................................................................ 386
6.16.5 What We’ve Learned ........................................................................................ 388
6.17 More on Arrays ................................................................................................................. 389
6.17.1 Length and Count.............................................................................................. 389
6.17.1.1 A Basic Example ............................................................................. 389
6.17.2 Foreach: A Reminder ........................................................................................ 390
6.17.3 Discovery .......................................................................................................... 393
6.17.4 Putting It Together ............................................................................................ 394
6.17.4.1 Breakpoint, Observing Inheritance ................................................ 395
6.17.5 What We’ve Learned ........................................................................................ 399
6.18 Out Parameter ................................................................................................................... 399
6.18.1 A Basic Example ............................................................................................... 401
6.18.2 Simple Sort (Bubble Sort) ................................................................................. 403
6.18.3 Simple Sort Proof .............................................................................................. 405
6.18.4 What We’ve Learned ........................................................................................ 406
6.19 Ref Parameter ................................................................................................................... 407
6.19.1 A Basic Example ............................................................................................... 407
6.19.2 Code Portability Side Effects............................................................................ 408
6.19.3 What We’ve Learned .........................................................................................410
Contents xiii

6.20 Type Casting Numbers ......................................................................................................411


6.20.1 Number Types ....................................................................................................413
6.20.2 Integers ...............................................................................................................413
6.20.2.1 Signed Numbers ...............................................................................414
6.20.3 Floating Point .....................................................................................................414
6.20.4 What We’ve Learned .........................................................................................415
6.21 Types and Operators ..........................................................................................................415
6.21.1 GetType() .......................................................................................................416
6.21.2 More Type Casting .............................................................................................417
6.21.3 Type Aliasing .................................................................................................... 420
6.21.4 Boxing and Unboxing ....................................................................................... 420
6.22 Operator Overloading ....................................................................................................... 422
6.22.1 A Basic Example ............................................................................................... 423
6.22.2 Overloading* ..................................................................................................... 424
6.22.3 Overloading < and > ......................................................................................... 426
6.22.4 What We’ve Learned ........................................................................................ 427
6.23 Controlling Inheritance .................................................................................................... 427
6.23.1 Sealed ................................................................................................................ 428
6.23.1.1 A Basic Example ............................................................................. 428
6.23.2 Extension Functions .......................................................................................... 429
6.23.3 Abstract ............................................................................................................. 430
6.23.3.1 A Basic Example ............................................................................. 430
6.23.4 Abstract: Abstract ..............................................................................................432
6.23.5 Putting This to Use ........................................................................................... 434
6.23.6 What We’ve Learned .........................................................................................435
6.24 Building A Game—Part 5 ................................................................................................ 436
6.24.1 Generating Rooms ............................................................................................ 436
6.24.2 What We’ve Learned ........................................................................................ 439
6.25 Leveling Up ...................................................................................................................... 440

7. Advanced ....................................................................................................................................... 441


7.1 What Will Be Covered in This Chapter ........................................................................... 441
7.2 Review .............................................................................................................................. 442
7.2.1 Moving Forward ............................................................................................... 442
7.3 Visual Studio .................................................................................................................... 442
7.3.1 The Debugger.................................................................................................... 443
7.3.2 Mono History .................................................................................................... 443
7.4 Function Overloading ....................................................................................................... 444
7.4.1 A Closer Look at Functions .............................................................................. 444
7.4.1.1 A Basic Example ............................................................................. 445
7.4.2 Function Signature ............................................................................................ 446
7.4.3 Different Signatures .......................................................................................... 447
7.4.4 Putting It Together ............................................................................................ 448
7.4.5 Not Quite Recursion.......................................................................................... 449
7.4.6 DrawWords........................................................................................................ 450
7.4.7 What We’ve Learned .........................................................................................453
7.5 Accessors (or Properties) ...................................................................................................453
7.5.1 Value ................................................................................................................. 454
7.5.1.1 A Basic Example ..............................................................................455
7.5.2 Set Event.............................................................................................................455
7.5.3 Read-Only Accessor ......................................................................................... 457
7.5.4 Simplification .................................................................................................... 458
7.5.5 What We’ve Learned ........................................................................................ 459
xiv Contents

7.6 Base Classes: Another Look ............................................................................................. 459


7.6.1 Generalization—Base Classes .......................................................................... 459
7.6.2 Specialization .................................................................................................... 461
7.6.3 Base ................................................................................................................... 461
7.6.4 Partial ................................................................................................................ 463
7.6.5 Protected, Private, and Public ........................................................................... 466
7.6.6 What We’ve Learned ........................................................................................ 467
7.7 Optional Parameters ......................................................................................................... 467
7.7.1 Using Optionals................................................................................................. 468
7.7.1.1 A Basic Example ............................................................................. 469
7.7.2 Optional Arguments ......................................................................................... 470
7.7.3 Named Parameters .............................................................................................471
7.7.3.1 A Basic Example ............................................................................. 472
7.7.4 Combining What We’ve Learned ..................................................................... 473
7.7.5 What We’ve Learned .........................................................................................474
7.8 Delegate Functions ............................................................................................................474
7.8.1 Delegates ............................................................................................................474
7.8.1.1 A Basic Example ..............................................................................474
7.8.2 Delegate Signatures............................................................................................476
7.8.3 Stacking Delegates .............................................................................................476
7.8.4 Using Delegates................................................................................................. 477
7.8.5 Updating Delegates ............................................................................................478
7.8.6 What We’ve Learned ........................................................................................ 480
7.9 Interface ............................................................................................................................ 480
7.9.1 Early Planning .................................................................................................. 480
7.9.1.1 A Basic Example ............................................................................. 481
7.9.1.2 Using Accessors .............................................................................. 483
7.9.2 Multiple Interfaces ............................................................................................ 486
7.9.3 IComparer ......................................................................................................... 488
7.9.4 Using IComparer ............................................................................................... 490
7.9.5 What We’ve Learned .........................................................................................491
7.10 Class Constructors Revisited .............................................................................................491
7.10.1 A Basic Example ............................................................................................... 492
7.10.2 When to Create a New Class ............................................................................ 495
7.10.3 What We’ve Learned ........................................................................................ 498
7.11 Preprocessor Directives .................................................................................................... 498
7.11.1 A Basic Example ............................................................................................... 499
7.11.2 UNITY_EDITOR ............................................................................................. 500
7.11.3 Mobile Development ......................................................................................... 502
7.11.4 Warning............................................................................................................. 504
7.11.5 Organizing ........................................................................................................ 505
7.11.6 What We’ve Learned ........................................................................................ 506
7.12 Try Catch Finally .............................................................................................................. 506
7.12.1 A Basic Example ............................................................................................... 506
7.12.2 Exception Messages .......................................................................................... 508
7.12.3 Custom Exceptions ............................................................................................510
7.12.4 Finally ................................................................................................................512
7.12.5 Try-Catch and Finally in Use.............................................................................513
7.12.6 What We’ve Learned .........................................................................................513
7.13 IEnumerator .......................................................................................................................514
7.13.1 Enumeration .......................................................................................................515
7.13.1.1 A Basic Example ..............................................................................515
7.13.1.2 What Doesn’t Work ..........................................................................517
Contents xv

7.13.2 Implementing IEnumerator ................................................................................517


7.13.3 What We’ve Learned .........................................................................................521
7.14 Generics .............................................................................................................................521
7.14.1 Generic Functions ............................................................................................. 522
7.14.1.1 A Basic Example ............................................................................. 522
7.14.1.2 Why T? ............................................................................................ 523
7.14.2 Making Use of Generic Functions .................................................................... 523
7.14.3 Generic Types ................................................................................................... 526
7.14.4 Var ..................................................................................................................... 527
7.14.5 Multiple Generic Values ................................................................................... 529
7.14.6 Anonymous Objects .......................................................................................... 530
7.14.7 What We’ve Learned .........................................................................................533
7.15 Events .................................................................................................................................533
7.15.1 A Basic Example ............................................................................................... 534
7.15.2 A Proper Event .................................................................................................. 536
7.15.3 EventArgs ...........................................................................................................537
7.15.4 Generic EventArgs .............................................................................................539
7.15.5 What We’ve Learned ........................................................................................ 540
7.16 Extension Functions .......................................................................................................... 540
7.16.1 Extensions ..........................................................................................................541
7.16.2 A Basic Example ................................................................................................541
7.16.3 Overloading with Extensions ............................................................................ 543
7.16.4 Magic Mirror .................................................................................................... 544
7.16.5 What We’ve Learned ........................................................................................ 546
7.17 Destructors ........................................................................................................................ 546
7.17.1 A Basic Example ............................................................................................... 547
7.17.2 Clearing Events or Delegates ............................................................................ 549
7.17.3 OnDestroy ......................................................................................................... 550
7.17.4 The Ugly Truth...................................................................................................552
7.17.5 What We’ve Learned ........................................................................................ 554
7.18 Concurrency or Co-Routines .............................................................................................555
7.18.1 Yield ...................................................................................................................555
7.18.1.1 A Basic Example ..............................................................................557
7.18.2 Setting Up Timers ..............................................................................................558
7.18.3 Arrays of Delegates ............................................................................................561
7.18.4 Stopping a Co-Routine ...................................................................................... 562
7.18.5 What We’ve Learned ........................................................................................ 565
7.19 Dictionary, Stacks, and Queues ........................................................................................ 565
7.19.1 A Basic Example ............................................................................................... 565
7.19.2 ContainsKey ...................................................................................................... 567
7.19.3 Stacks ................................................................................................................ 568
7.19.3.1 A Basic Example ............................................................................. 568
7.19.4 Movement Stack ................................................................................................ 569
7.19.5 Queues................................................................................................................571
7.19.6 What We’ve Learned ........................................................................................ 573
7.20 Lambda Expressions ......................................................................................................... 573
7.20.1 Anonymous Expressions ....................................................................................574
7.20.1.1 A Basic Example—Action Func and Expression ............................575
7.20.2 Lambda Expressions ......................................................................................... 577
7.20.3 A Queue of Lambdas ........................................................................................ 577
7.20.4 What We’ve Learned ........................................................................................ 578
7.21 Leveling Up ...................................................................................................................... 578
xvi Contents

8. Extended........................................................................................................................................ 579
8.1 What We’ll Be Covering in This Chapter ........................................................................ 579
8.2 Review .............................................................................................................................. 579
8.3 Readability .........................................................................................................................581
8.3.1 ?: Notation ..........................................................................................................581
8.3.2 If ........................................................................................................................ 582
8.3.3 Smell ................................................................................................................. 583
8.3.3.1 Comments ......................................................................................... 583
8.3.3.2 One Responsibility Rule ................................................................... 583
8.3.3.3 Duplicated Code ............................................................................... 584
8.3.3.4 Naming ............................................................................................. 584
8.3.4 What We’ve Learned ........................................................................................ 584
8.4 Git Revisited ..................................................................................................................... 584
8.4.1 Git Branch and Merge ....................................................................................... 585
8.4.2 Merge Conflicts ................................................................................................. 588
8.4.3 What We’ve Learned .........................................................................................591
8.5 Recursion ...........................................................................................................................591
8.5.1 A Basic Example ............................................................................................... 593
8.5.2 Understanding Recursion .................................................................................. 593
8.5.3 In Practice ......................................................................................................... 594
8.5.4 What We’ve Learned ........................................................................................ 601
8.6 LINQ ................................................................................................................................. 601
8.6.1 Lambdas and Arrays ......................................................................................... 601
8.6.1.1 A Basic Example .............................................................................. 601
8.6.2 Var ..................................................................................................................... 602
8.6.3 LINQ From ....................................................................................................... 603
8.6.4 Strange Behaviors in LINQ .............................................................................. 604
8.6.5 LINQ on Lists ................................................................................................... 605
8.6.6 Monsters Linq ................................................................................................... 608
8.6.7 What We’ve Learned .........................................................................................611
8.7 Reflection ...........................................................................................................................613
8.7.1 A Basic Example ................................................................................................615
8.7.2 Reflection MethodInfo .......................................................................................615
8.7.3 What We’ve Learned .........................................................................................618
8.8 Dynamic............................................................................................................................ 620
8.8.1 ExpandoObject ...................................................................................................621
8.8.2 Expando Reader ................................................................................................ 622
8.8.3 What We’ve Learned ........................................................................................ 623
8.9 Bitwise Operators ............................................................................................................. 624
8.9.1 Big Endian and Little Endian ........................................................................... 624
8.9.2 Signed or Unsigned ........................................................................................... 625
8.9.3 Bitwise Or | ....................................................................................................... 627
8.9.4 Enums and Numbers ......................................................................................... 627
8.9.5 Bitwise And & .................................................................................................. 628
8.9.6 Bitwise Exclusive Or ^ (xor) ............................................................................. 629
8.9.7 Setting Bitwise Flags ........................................................................................ 630
8.9.8 Bitwise Shortcuts | = and ^ = .............................................................................631
8.9.9 Bits in Numbers ................................................................................................ 632
8.9.10 Bit Shifting >> and << ..................................................................................... 632
8.9.11 What We’ve Learned ........................................................................................ 634
8.10 Attributes .......................................................................................................................... 635
8.10.1 A Basic Example ............................................................................................... 635
8.10.2 Custom Attributes ............................................................................................. 639
Contents xvii

8.10.3 Custom Attributes for Default Values............................................................... 639


8.10.4 Multiple Attributes ............................................................................................ 641
8.10.5 Putting Attributes to Work ................................................................................ 642
8.10.6 Attribute Flags .................................................................................................. 644
8.10.7 What We’ve Learned ........................................................................................ 645
8.11 Bitwise Math ..................................................................................................................... 646
8.11.1 Two’s Complement Unary Operator ~ .............................................................. 647
8.11.2 Bitwise Addition and Subtraction ..................................................................... 648
8.11.3 Bitwise Multiplication ....................................................................................... 650
8.11.4 Bitwise Tricks ....................................................................................................651
8.11.5 What We’ve Learned ........................................................................................ 652
8.12 Architectures and Organization ....................................................................................... 653
8.12.1 Planning Structure: Namespaces and Directories ............................................ 653
8.12.2 Divide and Conquer .......................................................................................... 654
8.12.3 Refactoring ........................................................................................................ 655
8.12.4 What We’ve Learned ........................................................................................ 656
8.13 Design Patterns ................................................................................................................. 656
8.13.1 Creational Design Patterns ............................................................................... 657
8.13.1.1 Singleton Pattern ............................................................................. 657
8.13.2 Structural Design Patterns ................................................................................ 657
8.13.3 Behavioral Design Patterns............................................................................... 658
8.13.4 What We’ve Learned ........................................................................................ 658
8.14 Continuing on Your Own.................................................................................................. 658

9. Stuff We Couldn’t Cover ..............................................................................................................661


9.1 The Extern Keyword..........................................................................................................661

10. Good Luck..................................................................................................................................... 663

Index ...................................................................................................................................................... 665


1
Introduction: What This Book Is About

This book was written as an answer for anyone to pick up a modern programming language and be pro-
ductive. You will be able to start a simple game in Unity 3D from scratch. By the end of this book, you
will have the basic skills to eventually become a capable unity game programmer, or at least know what
is involved with how to read and write some code.
You should have general computer skills before you get started. Come prepared; you’ll need a modern
Windows or OSX computer capable of running modern software with an internet connection. After this
book, you should be armed with the knowledge required to feel confident in learning more.
Each chapter has example code organized by chapter and section. We’ll try to make a fun project start-
ing with the basic functions of a typical game and we’ll see how the basic game can be expanded upon
and we’ll learn a bit about what’s involved with a larger project.

1.1 Why Read a Book: Why This Book May or May Not Be for You
You could go online and find videos and tutorials to learn; however, there is a disadvantage when it
comes to learning things in order and in one place. Most internet video or tutorial websites may gloss
over or dwell on a subject. You could skip ahead, but then what are you paying for?
Online content is often brief and doesn’t go into much depth on any given topic. It is incomplete or still
a work in progress. You’ll often find yourself waiting weeks for another video or tutorial to come out.
Just so you know, you should find the act of learning exciting. If not, then you’ll have a hard time con-
tinuing through to the end of this book. To learn any new skill, a lot of patience is required.
I remember asking an expert programmer how I’d learn to program. He told me to write a compiler.
At that time, it seemed rather unfair, but now I understand why he said that. It is like telling someone
who wants to learn how to drive Formula 1 cars to go compete in a race. In both cases, the “learning”
part was left out of the process.
It is very difficult to tell someone who wants to learn to write code where to begin. However, it all
really does start with your preparedness to learn. Your motivation must extend beyond the content of
this book.
You may also have some preconceived notions about what programming is. I can’t change that, but
you should be willing to change your thoughts on the topic if you make discoveries contrary to your
knowledge. Keep an open mind.
As a game developer, you should find the act of making a game, or simply learning how to make a
game, is just as fun as playing a game. If your primary goal is only to make that game that you’ve had in
your head for years, then you might have a problem. You may find this book a tedious chore if learning
C# is just something in the way of making your game.
Computer artists often believe that programming is a technical subject that is incompatible with
art. I find the contrary to be true. Programming is an art, much as literature and design are an art.
Programming just has a lot of obscure rules that need to be understood for anything to work.
No, programming is not too hard to learn. Writing a massive multiplayer online role-playing game
is quite hard. Learning how to write a simple behavior isn’t hard; like drawing, you start off with the
basics drawing spheres and cubes. After plenty of practice, you’ll be able to create a real work of art.
This applies to writing code, that is, you start off with basic calculations and then move on to the logic
that drives a complex game.

1
2 Learning C# Programming with Unity 3D

1.1.1 Do I Need to Know Math?


With complex rules in mind, does programming require the knowledge of complex mathematics? Unless
you are writing mathematical software, only a bit of geometry is required. Most of the examples here
use only a tiny bit of math to accomplish their purpose. Mathematics and programming do overlap quite
a lot in their methodology.
Math taught in schools provides a “best” solution. Programming results tend to behave a bit like a
math proof, but the proof isn’t just another bit of math. Rather, the proof of your code means that your
zombies chase after humans. A considerable amount of math required has been done for you by the com-
puter. It is just up to you to know what math to use, and then plug in the right variables.

1.1.2 Programming as a Form of Expression


There is a deeper connection between words and meaning in programming. This connection is more
mechanical, but flexible at the same time. In relation to a programming language, programmers like to
call this “expressiveness.”
When you write words in literature, you infer most if not all of the meaning. When programming
inference isn’t implied, then you’re free to define your own meanings for words and invent words freely.
One common merging of art and code appears within a video game. Anytime characters can react to
each other and react to user interaction conveys a greater experience. An artist, an animator, or an envi-
ronment designer should have some ability to convey a richer experience to the audience. A character
artist can show a monster to his or her audience; but to really experience the creature, the monster should
glare, grunt, and attack in reaction to the audience.

1.1.3 Games as a Stage with Lights


To do this, we’re going to use Unity 3D, a game engine. If you were to write a play, you would not have to
build the auditorium for your performance. Building lights and generating electricity to run them would
distract you from writing your play. Playwrights don’t often build lights and auditoriums from scratch,
and there are plenty of venues you could use as your stage, which is where game engines come in.
Game engines such as Unity 3D, Unreal Engine, Crytek, and even Game Maker, or Godot have a lot
of the basic game and math-related functions built for you. They are ready-built stages that allow you
to focus on making your game. The necessary code for rendering and physics have been done for you.

1.2 Personal Information


I started in the 1990s cleaning up sprites for Atari. Back then the job qualifications were basically having
Photoshop and a Wacom tablet. Oh, how times have changed!
I’ve worked for many different game studios, in both 2D and 3D, as an artist. I was exposed to many dif-
ferent software packages including Strata 3D on the Mac and Alias (PowerAnimator) on the SGI (Silicon
Graphics International), among many others. I loved learning new 3D modeling, rendering, and animation
applications. During my work as an artist, I used Maya, a popular 3D modeling package. While using
Maya, I was introduced to the Maya Embedded Language (MEL) script. Today I use Modo with Python.
After working as a character rigger and technical artist, I learned many programming languages
including MAXScript, MEL, and Python, which helped me to write tools for my fellow artists to use for
speeding up production.
I eventually moved into the job of programmer as my primary job function. I wrote prototypes for
Microsoft in Unreal using UnrealScript, a programming language much like JavaScript. This game studio
was run by John Gaeta and Peter Oberdorfer, who are award-winning movie special effects directors.
I began my career path as an artist, then became a technical artist, and finally a programmer.
This seems to be a common trend among a small variety of 3D artists like myself. I know quite a few
folks who have followed the same career path.
Introduction: What This Book Is About 3

During my transition from artist to programmer, I found that there was very little by way of help, docu-
mentation, web pages, or similar to complete the transition from an artist to a programmer. Many people
assumed that you were either a programmer to begin with, or not interested in making that transition.
After a long discussion, with my then girlfriend—now wife, about what a variable is, I decided it was
time to write a book.

1.3 A Brief History of Computer Programming:


How Programming Came to Be
Basic insight into the inner workings of your computer may help understand why and how some text gets
converted into computer instruction. The words that make up C#, or any other programming language
get converted into what is called byte code.
Skilled programmers tend to know more about the inner workings of their computer. Like any trade
or learned skill, the more you know about how your tools work, the better understanding of the entire
process you’ll have.
To understand why programming tools work as they now do stems from knowing how they used to
work. To understand how the current tools came about means knowing a bit of computer history and how
computers used to work.

1.3.1 Mechanical Computers


The first programmable computer is arguably the Babbage machine built by Charles Babbage in the
1820s. Made of tens of thousands of moving parts and weighing several tons, it was a gigantic mechani-
cal calculator that could display a 31-digit number. It was called the Difference Engine. in 1824 Babbage
won a gold medal from the British Astronomical Society, by using his gigantic calculator, for correcting
a set of tables used to chart the movement of planets through the sky.
In 1833, a countess named Augusta Ada King of Lovelace, commonly known as Ada Lovelace, met
Babbage at a party where she was introduced to the Difference Engine. Several years later, it became
evident that she understood the machine better than Charles himself. Despite being a housewife to the
Earl of Lovelace, she wrote several documents on the operation of the Difference Engine as well as its
upgraded version, the Analytical Engine. She is often considered to be the first computer programmer
for her work with the Difference Engine and its documentation.

1.3.2 Logic
In 1848, George Boole gave us Boolean logic. It would take nearly a century between the Difference
Engine and the first general programmable computer to make its appearance. Thanks to George, today
our computers count in binary (1s and 0s), and our software thinks in terms of true or false.
In 1887, Dorr Eugene Felt built a computing machine with buttons; thanks to him, our computers have
keyboards. In the 1890s, a tabulating machine used paper with holes punched in it representing 1s and 0s
to record the US census. Back then it saved US$2 million and 6 years of work.
There was a moment in time where a trinary computer existed. In 1840, Thomas Fowler created a compet-
ing mechanical computer which worked with −1, 0, and +1. Even as late as 1958 an electronic version was
created in the Soviet Union, but its usefulness lost out to binary which was easier to build and used less power.
In the 1900s, between Nicola Tesla and Alexander Graham Bell, modern computers were imminent
with the invention of the transistor. In 1936, Konrad Zuse made the Z1 computer, another mechanical
computer like that of Babbage, but this time it used a tape with holes punched in it like the tabulating
machine. He’d later move on to make the Z3 in 1941.

1.3.3 Computer Science


In the 1940s, Alan Turing, a British computer scientist, designed and built an encrypted message decoder
called the Bombe that used electromechanical switches for helping the Allied forces during World War II.
4 Learning C# Programming with Unity 3D

Between the 1930s and the 1950s, Turing informed the computer scientists that computers can, in theory,
solve anything calculable, calling this concept Turing completeness. All of these components were lead-
ing toward our modern computer.
In the mid-1940s, John Von Neumann demonstrated, with the help of his theory, that the computer
can be built using simple components. In this way, the software that controls the hardware can add the
complex behavior. Thanks to Tesla and Bell, the Z3 was made completely of electronic parts. It included
the first use of logic while doing calculations, making it the first complete Turing-complete computer.
In 1954, Gordon Teal at Texas Instruments introduced the silicon-based transistor, a key component for
building solid-state electronics that are used today in every computer.

1.3.4 The Origin of Software


The Z3 was programmed using Plankalkül, the first high-level programming language, invented by Zuse
shortly after he finished building the machine. In 1952, Grace Hopper created the first compiler software
that could translate human-readable words into machine operations. For the first time, the programmer
didn’t need to know how the transistors operated in the hardware to build software for the computer to
run. This opened computer programming to a whole new audience.
Between the 1950s and 1970s, computers and computer languages were growing in terms of complex-
ity and capability. In 1958, John McCarthy invented LISP (for LISt Processor) at the Massachusetts
Institute of Technology (MIT). Object-oriented programming appeared in Simula 67 in 1967. Imperative
and procedural programming made its appearance in Pascal in 1970.

1.3.5 Modern Computer Language


Bell Labs, started by Bell in the 1920s, hired Dennis Ritchie in 1967. In 1974, Ritchie published the
C programming language that has become the most popular computer programming language. In 1983,
Bjarne Stroustrup introduced C++ as “C with Classes.”
In 2001, Microsoft released C#, a combination of several programming language paradigms taking the
concepts from C, C++, LISP, Haskell, and others, and incorporating the ideas and concepts into a single
language. Today, C# is in its seventh major revision, whereas C++ is in its seventeenth revision. C# is
an evolving language having new features and concepts. Each revision makes the language more flexible
and most important, more expressive.

1.3.6 The Future of Computer Languages


The International Business Machines Corporation (IBM), a US company founded in the 1920s, is cur-
rently engineering new technology using particle physics discovered nearly a century ago. Modern spin-
tronics derived from the work of German physicists in the late 1920s.
Without particle physics and physicists such as Friedrich Hermann Hund, we wouldn’t understand
quantum tunneling, and the solid-state storage or universal serial bus (USB) thumb drive wouldn’t exist.
After the discovery and confirmation of the Higgs boson, you might ask “Why bother?” This question
might not get a good answer for another hundred years, but it’ll be worth it.
C# is not the last computer language; many others have been introduced just recently. Google has intro-
duced Kotlin specifically for developing apps for Android and Apple has introduced Swift for their iOS
platform. Microsoft, not to be outdone, introduced Q# for the future of quantum computing.
Using Q# for game development isn’t likely, at least for now. Programming languages have advantages
and disadvantages in different disciplines. The purpose of programming languages is to allow human
readable text to be converted into instructions that run on a computer’s CPU.

1.4 C#: A Flexible Programming Language


There are plenty of programming languages and each language was created to fulfill different roles.
Modern programming languages in use today are often described by their paradigms. A paradigm can
be explained as a sort of writing style; each style follows a different set of rules.
Introduction: What This Book Is About 5

1.4.1 C# Paradigm
In poetry, a haiku follows a 5-7-5 syllable tempo. A sonnet follows an iambic pentameter with 10 syl-
lables per line and rhymes in a specific pattern. Not all prose needs to end in rhyme like haiku, but many
do. Rhyming and tempo are paradigms used in poetry.
Modern programming paradigms include object oriented, functional, imperative, and logic program-
ming. Some programming languages use one or more of these paradigms together. For instance, F# is an
imperative, object oriented, and functional language. However, Haskell is purely functional.
C# (pronounced “see sharp”) is a combination of many different paradigms. As the name infers, C#
is a C-style language and primarily expressed using the object-oriented paradigm. How C# expresses its
different paradigms, and how you choose to write with it, is greatly up to you. Due to this flexibility, C#
offers a variety of uses reaching far beyond game development.
You may have heard of C++, another multiparadigm language. It is often used by programmers
who need detailed control over memory management and many other aspects of their final product.
This attention to detail and design is required to produce a refined final product.
C# is an imperative language, that is to say, it carries out operations in order. It is also object oriented,
so each object is allowed to do things on its own. Finally, it is also memory managed, aka garbage col-
lected, so your computer’s memory is organized for you. However, it does provide the flexibility to man-
age memory if you require that level of control. Just as well, C++ has recently added garbage collection
features to match C#.

1.4.2 Unity 3D: A Brief History of Game Engines


A game engine is basically software that provides many of the basic functions that are commonly used
to build a video game. Most game engines allow for rendering either 2D or 3D shapes by importing files
generated in a third-party software such as Blender, or 3D Studio Max or Maya, if you can afford them.
Another function often found in a game engine is the ability to write software for the game engine to run.
Unity 3D allows you to write software to run your game in C#.
3D game engines have a relatively short history in general computing. Their evolution has been hot
and fast. Starting around the 1990s, the 486 processor finally made its way into the general populace as
a processor fast enough to allow for everyone to play 3D games. Or at least pseudo-3D, 2D images drawn
in the shape of three-dimensional geometry. The technology for true 3D polygons would take a few more
years to make it onto the average person’s desktop.
Pseudo-3D was used to describe skewing and scaling flat sprites or 2D images to appear as though they
were in 3D space. True vertex transformation wouldn’t appear in popular video game engines on the PC
with games such as Quake 1 until 1995. This was greatly with the help of faster and enhanced graphics
processors. The modding community took Quake 1 and created many new additions and full conversions.
When powerful graphics processing units (GPUs) were introduced, it opened the floodgates for many
more 3D games to make their appearance. At first, these games functioned only on million-dollar com-
puters, but thanks to 3dfx, nVidia, Maxtrox, and ATi in the late 1990s, the GPU quickly took over being
a requirement for any 3D game. At that time, id Software, run by John Carmack, quickly came to the
forefront of the 3D gaming engine technologies that introduced many concepts such as spline surfaces,
complex shadows, and lighting with Quake 2.
Around 1998, Epic Games’ Unreal Engine and Valve Software’s Source Engine took off with the
launch of Unreal Tournament and Half-Life. More modding and conversions exploded onto the scene.
Many successful game modifications including Counter-Strike would go on to be one of the most played
online multiplayer games Valve Software would publish for years to come.
Before the availability of dedicated GPU and cheap RAM, computers didn’t have enough free resources
to support a game engine with editing tools. Often the game itself required so much of the computer that
there was little room left for any extras, such as particle editors or just-in-time compiling.
The modding community influenced business, making Unreal 2 and Source more modder friendly.
Garry’s Mod and Unreal Editor changed how game engines were made by adding tons of user-friendly
tools. Many users bought games specifically to “mod,” or modify, and create new games.
6 Learning C# Programming with Unity 3D

It’s no surprise that at this time many game engines would show up on the scene to cater to the mod-
ding community and the independent game developer. The large game engines found themselves top
heavy by focusing on large publishers. Companies such as RenderWare and Gamebryo found themselves
poorly positioned to compete against newcomers who focused on the independent developers such as
Unity 3D.
With the independent game developer in mind, many game engines focused on affordability and cross-
platform development. Tools such as Game Maker, Unreal Engine, and of course Unity 3D allowed
for many more people to learn and build games without the multimillion-dollar investment formerly
required for game production.

1.4.3 Why Use Unity 3D to Learn?


Making a game is more fun than making accounting software.
Having fun learning is more important as getting high test scores. After a test, you pretty much forget
the anguish of studying all night and what it was for. So there is no reason to make learning menial work
because studying should not be boring.
Once learning becomes entertainment, the lessons become something you want to remember. As a fun
experience, you remember the experience longer. I’ve found that learning how to write game artificial
intelligence (AI) and behavior is the best way to retain programming experience.
Programming lessons are often taught using stiff diagrams and simple math problems with little or no
immediate or obvious applicable use. How often have you actually needed to know where trains would
meet leaving two different cities at two different speeds? Experience shows that readers lose interest in
any example involving grade-point averages or a train leaving from New York.
Unity 3D offers examples by blowing things up. Calculus and vector examples are usually explained
using grids and lines. These concepts are lost by abstract pictures that lack meaning or application.
In this case, we’ll use math to solve for where a bad guy needs to throw a grenade in order to hit a mov-
ing target.
Finally, we’ll end up with both the knowledge that will help build a game and the skills needed to write
software outside of Unity 3D. With any luck, we’ll also have fun while learning. Some of these examples
may even help with your math classes.
Unity 3D also provides us with a complete set of tools we can use in our code quickly and easily. These
tools are a part of the Unity 3D application programming interface (API). The Unity 3D API is filled
with useful things such as commonly used routines, data structures, and classes. These parts of the API
are often called a library. We’ll learn what all that means in the remaining chapters.

1.4.4 How Does Unity 3D Use C#?


Not that long ago, Unity 3D supported three programming languages. First there was Boo, which
was given an accompaniment with JavaScript followed by C#. As of the latest version of Unity 3D,
C# is considered the primary, or first-class programming language. Both JavaScript and Boo have
been phased out.
In general, C# is gaining more support across many open source projects and has a growing user base.
The Dot Net Foundation which maintains .NET Core, is an open source project supported on multiple
platforms.
You might be surprised to learn that programming languages have version numbers. With each new
version of a language comes new features. As of the writing of this book, Unity 2018 and newer sup-
ports. NET 4.7 which is C# version 7.0. What does that mean? It’s not so simple, actually. Microsoft
created .NET (dot net): a framework or a standardized environment in which code can accomplish
common tasks with fewer commands. Using software without a framework often means reinventing
code that’s been written many times. The framework is also intended to be easily extended. Unity 3D
includes many game-related additions to help make building games easier.
Introduction: What This Book Is About 7

The progression looks something like this table:

January 2002 C# 1.0 .NET 1.0


April 2003 C# 1.2 .NET 1.1
November 2005 C# 2.0 .NET 2.0 Unity 3D 4.1
November 2007 C# 3.0 .NET 3.5
April 2010 C# 4.0 .NET 4.0 Unity 3D 5.6
August 2012 C# 5.0 .NET 4.5
July 2015 C# 6.0 .NET 4.6 Unity 3D 2017.1
March 2017 C# 7.0 .NET 4.7 Unity 3D 2018.1
August 2017 C# 7.1 .NET 4.7.1

As new versions of .NET were introduced, the language that went along with it was also upgraded,
though with different version numbers. Confusing? Yes. Unfortunately, to add to the confusion there’s
also three versions of .NET that include .NET Standard, .NET Framework, and .NET Core. The differ-
ences between these have no effect on what we’ll be learning in this book, so I’ll leave that research up to
you to do on your own. For now, we’re not so concerned with the version of either so long as the features
we learn about the C# language work within the context of Unity.
C# is used by a large community of general application programmers, not just game engineers. Unity
3D is also a cross-platform game engine, and a developer working on either OSX or Windows can both
share and use files for Unity 3D. C# is the same on both Mac and PC as well as Android and iOS for
mobile, so there is no concern over what operating system you’re going to use.

1.4.4.1 How to Tell Unity 3D What to Do


Unity 3D and Microsoft Visual Studio are going to be our integrated development environment (IDE).
IDEs come in many different forms. Many professional Windows programmers use Visual Studio, and
Apple developers usually use Xcode in OSX. Microsoft has also made Visual Studio available for OSX.
An IDE provides many tools that make programming easier by highlighting the lines of code and auto-
matically completing many commonly repeated tasks.
When it comes to learning about the nuts and bolts of programming, you’re basically learning about
how your computer thinks, or in more general terms, how your computer does what it is told. Somewhere
along the way you’ll realize how unintelligent your computer is and how smart good programmers are.
Programming is basically writing detailed instructions on what to do and when. In short, get data then
do something with the data. A big part of programming is figuring out where to get information; we’ll
call this data management. Then you use logic to determine what to do with the data once you have it.
Programming logic is organized into algorithms found in statements organized into functions.
Unity 3D provides tools to move characters and objects with your code. These assets all live in the
project directory as separate files or entities that you can control through code. Unity 3D provides scenes
to give structure to your assets—3D models particle systems for explosions, fire, and other special
effects—and tools to create and edit all these things. It is largely up to you to learn these different sys-
tems on your own, but this book covers the code part of things.
Unity doesn’t provide a set of 3D modeling or 2D image editing tools. For this you’ll want to find
something like Blender 3D which is free or use another 3D modeling and texturing tools set. You’ll also
want to look into Gimp or Dogwaffle to paint 2D images to import into Unity.

1.5 What Is Programming?


It’s all about writing code.
Programming is a process in which we organize data and use logic to do something with the data.
The data is everything a computer can store; they can range from numbers to zombie characters in a
video game. You do this by writing text into many files called source code. Source code written into text
files replaces punch cards used by the computing machines half a century ago.
8 Learning C# Programming with Unity 3D

When data is combined with logic and then written into a single file, it’s called a class. Classes are also
data and, as such, can be managed with more logic. Classes are used to create objects in the computer’s
memory and can be duplicated to have a life of their own.
Classes are used to build objects. Each piece of data within the class becomes a part of that object.
Different chunks of data inside of a class are called class members. Class members can also be chunks
of logic called functions or methods. If that is not clear right now, don’t worry we’ll go into detail on that
in Chapter 2.
Objects created from a class are called instances. In a game with a horde of zombies, each zombie is
duplicated or instanced from a zombie class. Each zombie has unique values for each attribute or data
element in the class. This means hit points and positions are unique for each duplicate zombie object.
Similar to families, objects can inherit properties from one another. The child, sometimes called a
subclass, inherits attributes from its parent. For instance, the child created from a zombie may inherit
the parent’s hunger for brains. To be useful, the child zombie can also add new objects and change the
objects it inherited from its parent class. As a result, now the child zombie might have tentacles that the
parent didn’t have.
Objects talk to each other through events and messages. Shooting at zombies can create an event, or
in programmer terms, it “raises” an event. The bullet impact event tells the zombie class to then take
necessary steps when hit by a bullet. Events command the class to take actions on its data, which is where
functions come in.
Functions, also known as methods, are sections of logic that act on data. They allow your class to cre-
ate additional events and talk to yet more objects. As the player presses the trigger and moves the joystick
around, yet more events can be raised and messages can be sent. Events and messages allow the player
to interact with your world; logic events and objects together build your game.

1.5.1 What Does C# Look Like?


A game in Unity 3D is made up of many different C# files. Each file can be referred to as a “class” and
ends with the .cs file extension (which stands for C Sharp). This isn’t a class like those that are taught. It is
more akin to biology in which classes are categories that define different groups of organisms.
Introduction: What This Book Is About 9

A basic game might have several different classes, each one named according to its purpose. Each one of
these classes contains the text that is your actual C# code. Just so you know, by default Windows will hide
the extensions of your files. As a result, if classes appear as simply Weapon or Monster and there is no exten-
sion.cs, then you should go into the Tools menu, select Folder Options/View, and enable file extensions.
As an exercise, we’ll be building a simple game involving monsters and shooting. We’ll start with a
basic skeleton with simple object classes and simple movement behaviors. From this we’ll build on some
basic events when the player presses keys, moves the mouse, and shoots at targets.

1.5.2 Learning to Copy and Paste


A good artist creates, a great artist steals.
Code examples exist for you to copy and paste into your project. The content for this book and all down-
loadable content are no different. You’ll have to understand what the code does and fit it to your needs.
Every programmer copies code habitually. Whether it is from code written in a previous project, or
code found on the internet. Programmers copy code to learn. Of course, it’s still important to know how
to write new code from scratch; most programming classes in school will prefer that you do this for all
your assignments. With practice, being able to recognize what to copy and where to paste is something
you’ll only know from writing code to begin with.
Programming is a constant learning process. It is a language to command computers. Anytime you
learn a new language, there will be plenty of words which you’ll have to look up. Add to this the fact that
every programmer gets to make up new words, and you’ve got a language that always needs a dictionary.
This is one reason why programmers are usually fast typists. This is also cause for programmers to be
picky about what keyboard they prefer.
In most cases, the projects in this book will be in some state where you can read text that is already
in place. Most of the projects are in a more complete state where you’ll be able to run them and see an
intended result.
As a programmer, I’ve gotten used to searching the internet for example code. Once I’ve discovered
something that looks useful, I copy that code and paste it into a simple test case. After I’ve wrapped my
head around what the code is doing, I rewrite it in a form that is more suited for my specific case.
Even if the code involves some fun trick, I’ll learn from that code. As I learn new tricks, I grow as a
programmer. The only way to learn new tricks is finding the necessity to solve a new problem for which
I haven’t already figured out a solution. Finding solutions to problems is a fundamental part of being a
programmer.

1.5.3 Iteration
Programming is an iterative process. It’s irregular to write an entire class or function without test-
ing it many times. The general flow is to write a part of a function, test it, add a bit more, test again,
and repeat until the function accomplishes its goal. Even then, many functions often work together in
sequence. Often, you’ll write a function, verify that it’s working, write another function, and verify some
more. Then you’ll have them work with one another and test again, all the while you’ll be changing and
re-arranging your code to fix mistakes.
Most of the time you’ll start off with a bunch of placeholders until you get around to finishing your
code. As you’re adding features and finishing code, you’ll have chunks of work that aren’t finished.
This is a part of game development and a part of learning as well.

1.6 Compiling: Turning Words into Computer Instruction


To test and use your code, it is time to compile. Compiling is the process of taking all of your source
files and building bytecode. Unity 3D uses .NET, as it is a compiler to generate the bytecode, but Unity
3D does this automatically. Mono is an open-source compiler that runs on many different processors and
operating systems.
10 Learning C# Programming with Unity 3D

The combination of the central processing unit (CPU) and operating system is often called a platform.
Each platform requires a unique native machine code to execute or run. Building code for each platform
is called a target. Unity 3D converts the bytecode into a native machine code and can target Mac, PC,
Android, and iOS.
Native machine code is the set of instructions that directly talk to the CPU and operating system (think
of the holes punched into a card and fed to a machine). Unity 3D is a simple way to generate the complex
set of instructions for your computer to run. Code that talks directly to the hardware is referred to as
“low-level” programming.
There are layers of software between you and the computer’s hardware. When writing C# for games
using Unity 3D, your code is compiled by .NET. Unity 3D then takes the bytecode from .NET and com-
piles a target platform into a native machine code.
Both Unity 3D and .NET are the layers underneath your code and the computer’s hardware, putting
you on a higher level. This is often referred to as a computer layer abstraction. That is why C# is usually
considered a high-level programming language.
Programming at a lower level requires much more knowledge of memory management and a wide
variety of other APIs that might include graphic cards, physics, sound, and everything else that runs a
game. Writing for a layer involves an in-depth knowledge of both the layers below and above the one
you’re writing for.
The computer hardware, such as CPU, graphics card, memory, and storage, live on the lowest level.
Above them is the basic input/output system (BIOS) and software that starts the hardware when you
press the Power button. Above that is your computer’s operating system and drivers that talk to the hard-
ware. Finally, Unity 3D lives above the operating system and your code lives above Unity 3D.
That is why we’re going to use Unity 3D to write games and not start from scratch in raw C++.
Otherwise, we’ll have to spend a few years learning about physics, rendering, and assembly language or
the instruction set that your CPU uses to operate.

1.7 What We’ve Learned


We should have an idea of what C# is, and what is required of you to learn it. If you haven’t done so
already, download and install the required software.
Unity 3D is free; there are no fees or royalties that need to be paid to the Unity 3D developers so long
as you’re not building any commercial product. Git is also free to use and you’ll need that to follow the
chapters on Git.
You’re even allowed to deploy your projects on anything Unity can target. Until you’re ready for a
commercial launch, the free license is all you’ll need for learning and sharing your game with your peers
on your computer.
Optionally, you may also want to setup an account on the Unity 3D site if you want to download and
use any of the assets from their Asset Store. A Visual Studio Community account and a GitHub account
may also come in useful later.

1.8 Leveling Up: The Path Ahead


The computer systems in place today have evolved from machines with moving parts. Telling these
machines what to do involved setting knobs and switches. Today, we use text organized into statements
and logic.
It is difficult to imagine a time when computers were so slow that simple calculations involved waiting
for clattering switches to finish moving around to produce an answer. Today, computers complete many
billions of calculations in a single second. All too often, we take for granted that computers rely on the
same concepts and systems engineered nearly a century ago.
Understanding how computers work puts into context how we write software. It is possible to write
software without this understanding, but it is harder to appreciate how it works.
Another Random Scribd Document
with Unrelated Content
GOAL.

GOAL—1st, The boy who stood at the centre of each end during a
game of football, and acted as umpire. 2d, The score made
(three) when the ball was kicked between his legs, or over his
head without his touching it.
GOMER—1st, A pewter dish; 2d, A new hat. One of the Commoner
Peals (v. inf.) was “Gomer Hats.”
GOWNER—The Goal (v. sup.) at football stood with his legs
stretched out, and a gown rolled up in a ball at each foot. When
the ball was kicked over either of these gowns, without Goal’s
touching it, this counted two for the party who kicked it, and was
called “a Gowner.”
GREASER—A mode of torture performed by rubbing a boy’s head
hard with the knuckles.
GROTIUS TIME—From seven p.m. to a quarter before eight on
Sundays, in Cloister time, when Sixth Book and Senior Part
went into school to translate the work of that author.
GROUND ASH—A young ash sapling.
GUTTER—An abortive attempt at a Header, (v. inf.,) ending in the
performer falling flat on the water, instead of going in head-
foremost.

H.
HAVES—Half Boots.
HERE—The call used by Commoner Præfects when they required
the attendance of a Junior.
HIGH-LOWS—Very thick low shoes, not half boots.
HILLS—St Catherine’s Hill, a green hill about one mile and a-half
from College, about five hundred feet high, and near the top
surrounded by a deep trench, the remains of an old Roman
camp. The boys had to ascend this twice a day on whole
Holidays and Remedies, once before breakfast, and again at
half-past two. In the summer they also went out in the evening,
but did not ascend Hills, but disported themselves below. These
sorties were called “going on to Hills,” the evening expedition
being called “Underhills.”
HISS, THE—The signal given at the commencement of school hours
when a Master was coming in.
HOLLIS—An oval pebble.
HOUSLE—To hustle.
HOT—Football term. In Twenty-two and Twenty-two, (v. inf.,) when
the ball went out of bounds, it was brought in and placed
between the two sides, who all clustered up close round, with
their heads down, each party, by weight and kicking, trying to
force the ball through the other. This mêlée was called a “Hot.”
HOT END—A half-burnt faggot stick, with one end red hot.
HUFF—Very strong College ale.
HUSKY—Gooseberry fool with the husks in.

I.
INFERIOR—Any boy not a Præfect.

J.
JACK—A large leather vessel for beer.

JACK

JORAM—A tin can for beer used in Commoners.


JUBILEE—Any time when there was nothing to do, either in the way
of lessons or fagging.
JUNKET OVER—To exult or triumph over another person in a
friendly manner,—e.g., “I Junket over you, old fellow; I have
leave out to-morrow.”
JUNIOR—All Inferiors except the seven Candlekeepers and Senior
Inferior. The word “Fag” was never used as a substantive except
in combination with another word, as, “Breakfast Fag.” The verb
and participle were in common use.

K.
KICK IN—In a game of football the bounds on each side were kept
by a line of Juniors, whose duty it was to kick the ball in again
whenever it passed outside the line.
KICK OFF—When the football was taken in hand and then kicked
into the air; which was done after each Schitt, (v. inf.,) Gowner,
(v. sup.,) or Goal, by the losing side, and whenever a ball that
had been kicked up in the air had been caught by one of the
other side.
KICK OVER—To kick a ball up in the air, when it is rolling along, or
lying on the ground. Considered very bad play.
KID—Cheese.

L.
LAUNCH—To drag a boy out of bed, mattress, bedclothes, and all.
LOB—A Tice. It will be observed by cricketers that this is not the
ordinary meaning of the term, which, I believe, generally
denotes any kind of slow, underhand ball. At Winchester a “Lob”
may be delivered by a swift, round-hand bowler.
LOBSTER—To Cry.
LOCKBACK—A Holiday or Remedy, on which, from bad weather or
any other cause, the boys did not go on to Hills, but remained
on the school side of Seventh Chamber Passage gate.
LOCKS AND KEYS—Commoner Peal, (v. inf.)
LOGIE—Sewerage.
LOG POND—A Sewer.
LONG BOX—A deal box for holding bats, stumps, and balls.
LONG DISPAR—Part of a loin of mutton.
LONG FORK—A stick used as a toasting-fork.
LONG GRASS—All meads except the paths and Turf, (v. inf.)
LONG HALF—The Half which commenced in February, and ended
in the middle of July.
LONG MEADS—A field between Sick-house and Commoners, now
thrown into Meads. The time after dinner on summer evenings,
when we went on to Underhills, was also so called.

M.
MADE BEER—College swipes bottled with raisins, sugar, nutmeg,
and rice, which made it “up.”
MAKE—To appropriate any article.
MASTER—The title by which farmers, labourers, bargees, &c., were
addressed by the boys.
MATER—Mother.
MESS—The Præfects’ tables in Hall were called “Tub, Middle, and
Junior Mess” respectively. The boys who dined at each were
also so named. Any number of boys who habitually breakfasted
together were so called, with some distinguishing prefix, such as
“Deputy’s Mess.” In Chambers, tea was called “Mess;” as was
also the remains of a joint of meat. Lest the reader should make
a “Mess” of all these different meanings, I will give a sentence in
which they shall all figure: “Look there, Junior Mess has sat
down at Tub Mess, but as they will find nothing left but a Mess,
they had better go down to Chambers, as Mess is ready.”
MIDDLE CUT—A thick slice out of the centre of a leg of mutton.
MIDDLE PART THE FIFTH—Generally called Middle Part; the next
Part below Senior Part the Fifth.
MILK HOLE—That part of a canal immediately below the lock-gates,
where a hole has been formed by the rush of water from the
lock.
MONEY AND DIRECTION ROLLS—Commoner Peal, (v. inf.)
MOUSE DIGGER—A miniature pickaxe.
MUG—To read hard; also to pay great attention to anything; any one
cleaning and oiling a bat was said to “Mug” it; a boy with
carefully greased and brushed hair was said to have “mugged”
hair.
MUSTARD AND PEPPER KEEPER—An appointment in the gift of
Præfect of Hall, which exempted the holder from Watching out
at Cricket or Kicking in at Football.
MUTTONER—A blow from a cricket-ball on the fingers, the bat being
at the time clasped by them.

N.
NAIL, TO STAND UP UNDER THE—The punishment inflicted on a
boy detected in a lie; he was ordered to stand up on Junior Row,
(v. inf.,) just under the centre sconce, during the whole of school
time. At the close of it he received a “Bibler,” (v. sup.)
NESTOR—Any boy who was past eighteen, or was old for his
position in the school, or who was known to be much older than
he looked.
NIPPERKIN, THE—A large stone jug for beer, of which there was
one in each Chamber.
NON HUSKY—Gooseberry fool without the husks.
NON LICET—Used as an adjective, and applied to things which it
was considered improper to use. A gate opening out of Meads
was called “Non Licet Gate,” from a tradition that it was only
opened when a boy was expelled.

O.
OFF BAT—The station of one of the field in a cricket match, called
by the outer world “Point.”
ON—The word given by the Præfect of Hall for the boys to start to or
from Hills, or to Cathedral. When any person or thing of
importance was known to be likely to meet the boys when on
Hills, the word was passed that he, she, or it was “On,”—e.g.,
“Ridsworth On,” “Snobs On,” “Badger On,” &c., &c.
ORDER YOUR NAME—An order given to a delinquent by the Head
or Second Master, which was carried out by the boy requesting
the Ostiarius (v. inf.) to do so, the consequence of which was,
that at the end of school that officer presented to the Master the
victim’s name on a “Roll,” (v. inf.,) who forthwith received a
“Scrubbing,” (v. inf.) When the words “to the Bible Clerk” were
added, the business was confided to that officer, who, with the
Ostiarius, officiated at the subsequent ceremony, which in this
case was called a Bibler, (v. sup.)
OSTIARIUS—An office held by the Præfects in succession. The
duties were, to keep order in school, collect the Vulguses, and
prevent the boys from shirking out. It is also the official title for
the Second Master.

P.
PACKING-UP—Commoner Peal, (v. inf.)
PART—Fifth Book was divided into Senior, Middle, and Junior “Part.”
PARTY ROLL—Commoner Peal, (v. inf.)
PATER—Father.
PAX—Be quiet; Leave off. Also, a particular friend.
PEAL—Epigrammatic praises or critiques in Latin, Greek, or English,
on the Præfects, chaunted by Cloisters, just before school, at
the commencement of Cloister Time. Also, on the three last
Sundays of each Half, immediately after dinner, Commoners
chaunted one of the following “Peals,” Packing up, “Party Rolls,”
and “Money and Direction Rolls.” They also had other Peals
which they emitted prior to the ceremony of “Sticking up,” (v.
inf.,) which were, “Locks and Keys,” “Boots and Leathers,” and
“Gomer Hats.” The different bells that were chimed for Chapel
were designated “First Peal,” “Second Peal,” &c.
ΠEMΠE Mῶρον Προτερον (“Send the Fool farther”)—An imaginary
book, in search of which a new boy was bandied about from one
to another.
PERCHER—A mark (⸺|—) put after a boy’s name on a “Roll,”
which showed that he had been absent from Chapel or Hills
without leave; or that he had not done his Verse or Prose Task,
or Vulgus. It was also often put by a Master in the margin of
Gags, or a Verse or Prose Task, to indicate gross errors.
PITCH UP—To make a crowd; also to associate with any one in
particular. As a substantive, it means a crowd, a number of
things, and a companion.
PLEDGE YOU—An expression used when a boy wished to secure
the next turn at anything which was in the use, enjoyment, or
occupation of another, such as the next drink from a bob of beer,
the next read of a newspaper, or the next occupation of a seat.
PLANT—To kick a football against a person.
PLANTER—A blow from a football.
PONTO—A ball made of hot bread kneaded hard.
POSERS—Two Fellows of New College who assisted at the
examination at Election.
POT—A canal lock; the one just under Hills was generally meant
when the word was used.
PRÆFECTS—The eighteen senior boys in College, and the twelve
senior in Commoners. The ten senior of those in College were
said to be in “Full Power,” and took the office of Bible Clerk in
rotation; they all had the power of fagging the Juniors, but those
not in full power were supposed not to have the right of fagging
on the School side of Seventh Chamber Passage; practically,
however, they always did. One of the Senior Præfects was
called Præfect of Hall, and was responsible in a great measure
for the conduct of the boys out of school. His duties and
privileges were numerous. There was also a “Præfect of Tub,”
who was supposed to see that the dinner was properly
distributed; a Præfect of School, who had the care of that
building; and two Præfects of Chapel, who, during alternate
weeks, called names in Chapel. There were fees attached to all
these offices; and all the Præfects had a certain number of boys
allotted to them as Pupils, each of whom paid one guinea each
Half.
PROSE—To lecture. As a substantive, a lecture.
PROSE TASK—A piece of Latin prose composition, which all the
boys had to do once a week.
PRUFF—Hard, sturdy, insensible to pain, obstinate; a corruption of
“Proof.”
PULPITEERS—In Cloister time, Sixth Book and Senior Part went up
to Books together, and were so called.

Q.
QUARTER OF PAPER—A quarter of a sheet of foolscap, on which
the Prose and Verse tasks were always written.
QUILL—To endeavour to curry favour with any one.

R.
RABBITER—A blow on the neck with the side of the hand, similar to
the coup de grâce ordinarily given by a keeper to put a rabbit
out of its misery.
RACK—Part of a neck of mutton.
RAMROD, RAYMONDER—Names given to a ball bowled all along
the ground.
READER—An office in the gift of every Præfect in senior Fardel,
which excused the recipient from watching out at Cricket. His
business was to read out aloud the translation of any book his
Master was cramming for Election examination.
READING SHELF—A shelf with a drawer fixed inside the head of a
boy’s bed, on which to place a candle for nocturnal studies.
REMEDY—A kind of mitigated holiday, of which there was always
one, and, generally, two a week. The boys went into school
twice in the course of the day for an hour, (Books Chambers, v.
sup.,) but no Master was present. A Remedy was not a matter
of course, but the Head Master was always asked by Præfect of
Hall to give one while he was walking up and down “Sands” (the
pavement of Quadrangle under Chapel windows) before
morning chapel: if he intended to grant the request, he gave to
the suppliant a ring engraved with the words, “Commendat rarior
usus.” This ring he wore till the following day, and returned to
the Head Master at Middle school.
REMISSION—When, owing to a Saint’s day having fallen on the day
previous to that on which a Verse or Prose task or Vulgus was
due, the boys were excused from doing it, there was said to be
“Remission” from it.
ROKER—Anything wherewith to stir up anything else.
ROLL—Any list of boys’ names. “The Roll” par excellence is the list
of the boys who have passed their examination for New College,
and of those who are to come in to Winchester. There is also a
Roll printed every November, which contains the name of every
one connected with the School, from the Warden to the
Choristers. The lists from which the Præfects of Hall and Chapel
called names; the papers on which the names of the absentees
on such occasions were written; the papers on which were
written the “Standing up” (v. inf.;) the lists of the boys who had
leave out on a Saint’s day; the papers put on the Master’s desk
when boys wished to go out of school; those handed to the
Master at the close of school by the Bible Clerk or Ostiarius with
the names of delinquents, and many other similar papers, were
all called “Rolls.”
ROUSH—A rush or charge by any man or beast, or of water.
ROWS—The fixed benches at each end of School, called
respectively Senior, Middle, and Junior Row.

S.
SS. AND TREES—A scratch game of Football without chosen sides,
or kicking in, so called after the goals, which were, at one end,
two iron clamps fixed in the wall, shaped like the letter S, and at
the other two trees.
SCADGER—A Ruffian.
SCALDINGS—A call of warning to get out of the way.
SCHEME—A method adopted by the boys for calling themselves in
the morning. It was managed by cutting the Functior down to a
length calculated to burn till the time required; paper was then
placed round the socket, and a string attached to it, the other
end of which passed through the head of a bed, and held
suspended over the head of the sleeper a weight of books or
cup of water. When the rushlight burned down it ignited the
paper, which burnt the string, and so caused the weight to fall on
the head of the boy beneath.
SCHITT—The score made (one) when the Football was kicked
between the goal and the last of the line of the kickers in.
SCOB—An oaken box with a double lid. All the College boys and a
few of the Commoner Præfects had one each, at which they sat
in School. So called from the word Box spelt phonetically
backwards.
SCONCE—To deprive a person of anything.
SCRAPE OUT—When a Præfect wished to go out of School, he
scraped with his foot till he got a nod from the Master.
SCRUBBING—A flogging of four cuts.
SCRUTINY—At the commencement of Election week, the Electors
summoned the seven Senior and seven Junior boys, and
inquired of them if they had any complaints to make with regard
to the arrangements made for their comfort.
SEMPER—Always. A very common prefix, e.g., a boy was said to be
Semper Continent, Tardy, or ex Trumps if he was often at Sick
House, or late for Chapel, or habitually went up to Books without
having looked at his lessons. An official, who was always
present at the College meetings, went by the name of “Semper
Testis.”
SENIOR PART, THE FIFTH—The Part next below the Præfects,
generally called Senior Part.
SHIG—A Shilling.
SHORT HALF—The Half-year commencing in September and
ending at Christmas.
SILVER FORK—A wooden skewer used as a chop-stick when forks
were scarce.
SINES—The loaves provided for breakfast in Commoners, probably
derived from “Sine,” (without,) as the Juniors so often went
without them.
SIX AND SIX—A game at Football, with six on each side.
SKIMMER—A method of entering the water when bathing; by just
skimming beneath the surface, and rising again immediately.
SKIN—To take off a Jersey by pulling it inside out over the head.
SKIRMISHING ON—Running home from Hills when it came on to
rain.
SNACK—A small Fives ball.
SNAPPING UP FOR FALSE QUANTITIES—When up at books, if
any boy, when translating, made a false quantity, any other boy
(however low down in the part) who could first correct him was
allowed to go up above him. If, however, the Snapper up was
himself wrong, he had to go to the bottom of the part.
SOCIUS—A Companion. Each boy was obliged to walk with one
when going to or from Hills or Cathedral.
SOCK—To hit hard at Cricket; also, to win; in the passive voice, to
be beaten.
SOG—A Sovereign.
SOROR—Sister.
SPITE—To dislike a person, and treat him accordingly.
SPITING GABELL—When a boy suffered some injury himself, in
order to spite another person; or having in some way injured
another, received punishment, he was said to be “Spiting
Gabell.” Dr Gabell was formerly Head Master, and the extreme
inexpediency of attempting to annoy him gave rise to the
proverb.
SPLICE—To throw.
SPORT—To give away; also, to display any article of dress.
SPREE—Conceited, Cocky, Giving himself airs; when applied to a
person; Smart, Stylish, when to a thing.
SQUISH—Very weak tea.
STANDING-UP WEEK—During the last week of Long Half, all the
boys, except Sixth Book and Senior Part, had to say a number
of lines by heart in eight lessons, which they were supposed to
have learnt in the course of the previous year; this was called
Standing up. Marks were given according to merit, and these
marks had a very material effect on the respective position of
the boys in their Parts.
STICKING UP.[18]

STICKING UP—On the three last Fridays of each Half, a boy was
selected by appointment of Commoner Præfects and
Coursekeeper, and placed on the top of “Toys” (v. inf.) in their
Hall, and was pelted with “Pontos” (v. sup.) by the rest. The
following Peals were chanted previously, one on each day:
“Locks and Keys,” “Boots and Leathers,” and “Gomer Hats.”
STUCKLING—A kind of mince-pie made of minced beef, caraway
seeds, and apples, always served at the Election dinners.
SUM—I am. The answer made by each boy when names were
called.
SUPERANNUATE—A boy who was obliged to leave at Election,
owing to his being past eighteen years of age. “Founders” were
not “Superannuate” till they were twenty-five.
SUS—The Juniors tea generally drunk out of a pint cup when in bed.

T.
TAG—A Football term. When a player has kicked the ball well
forward, and has followed it, if it was then kicked back again
behind him by the other side, he was then obliged to return to
his original position with his own side. If the ball had, in the
meantime, been again kicked in front of him, before he regained
his position, and he was to kick it, it would be considered unfair,
and he would be said “to Tag.”
TARDY—When a boy was too late to answer “Sum,” names being
called.
TEEJAY—To take an interest in, and protect any one. The boy
protected was called a “Teejay.” Derived from the French,
Protéger, to protect.
THICK--Stupid; very intimate. Used also as a substantive, thus a
Dunce.
THOKE—To lie in bed late in the morning. As a substantive, the act
of lying in bed late.
THOKE UPON ANYTHING—To dwell with satisfaction on a future
pleasure.
THOKER—A thick piece of bread dipped in water, and then baked in
the ashes.
TIN GLOVES—A new boy was fitted with a pair, by having the backs
of his hands scored backwards and forwards two or three times
with a “Hot End” (v. sup.) The supposed object of the ceremony
was to enable the victim to handle “Hot Ends” with impunity.
TIN GLOVES.

TIZZY—A Sixpence.
TIZZY POOLE—A Fives ball; so called, because they cost sixpence,
and were sold by the Head porter, whose name was Poole.
TOEFITIE—To secure the toe of a sleeper in a noose of string.

TOEFITIE.
TOE PAN—A large basin of red earthenware placed in each
chamber, for washing the feet in.
TOE PAN BOILER—A tin vessel for boiling water, containing about
three gallons.
TOLLY—A tallow candle.
TOTHER SCHOOL—Any school not a public school.
TOYS—Bureaux in Chambers and Commoner’s Hall. Each boy had
one, at which he sat during
TOY TIME—The period between dinner time and evening Chapel.
TUB—A chest in Hall, into which the Dispars not taken by the boys
were put.
TUB MESS—The table at which the Senior Præfects sat in Hall.
TU DOCES—A (Thou) Teachest.
TUG—Old, Stale.
TUGS—Stale News. A common remark when a boy related anything
known previously to the hearer.
TUNDING—A thrashing with a ground-ash inflicted by a Præfect.
When any grave offence had been committed, and it was
administered by Præfect of Hall on the raised dais at one end of
Hall, it was called a “Tunding on top of Hall.”
TURF—A part of Meads, almost exclusively kept for Præfects to play
Cricket on, and for the matches.
TWENTY-TWO AND TWENTY-TWO—A game at Football, with
Twenty-two on each side.
TWOSTER—A stick spirally indented by a stem of ivy having grown
round it.

V.
VALET—Every Præfect had a Junior in Chambers who acted in this
capacity, made his tea or coffee, carried his things through from
Chambers to School and back again, and looked after him in
general.
VARYING—A short extempore composition in Latin verse, done
without the aid of any books. It was always the last thing done at
the close of the Election examination.
VERSE TASK—A composition of Latin verse done once a week by
all the boys.
VESSEL OF PAPER—Half-a-quarter of a sheet of foolscap paper.
VOLUNTARY—A copy of verses written occasionally by some of the
boys in Sixth Book and Senior Part ex proprio motu.

W.
WARDEN’S PROG, (Progress)—The visitation of the College
estates by the Warden and Fellows.
WASHING DRAWER—An oaken dressing-case.
WASHING STOOL—The table at which each Præfect sat in
Chambers.
WASHING STOOL.

WATCHING OUT—Fielding at Cricket. When a Junior made a catch,


he was let off for the rest of the day.
WORSTEDERS—Very thick worsted stockings, worn at Football.

Y.
YOLLY—Yellow; also, a Postchaise, from that being their usual
colour.

YOLLY.
FOOTNOTES
[1] Three masters’ houses, for the reception of boys, have
recently been opened outside the college walls.
[2] Since the above was written, I have been shown a little
book, entitled “Ups and Downs of a Public School,” in which many
of the incidents alluded to by me are very graphically described,
especially “the play,” “the town and gown row,” and “the
examination in election week.”
[3] This word being hitherto unwritten, I have endeavoured,
unsuccessfully, to spell phonetically. It is derived from the French
proteger, and the last syllable is pronounced as in that word. Its
meaning is somewhat the same as that of the French word, but
implies rather a greater amount of care and interest.
[4] The beds in Seventh were different from those described in
Fourth, being made of iron, without any canopy, with deal boards
at the side to keep the mattresses in their places.
[5] See “Ups and Downs of a Public School.”
[6] On Sundays the boys went to Cathedral, instead of Chapel,
at half-past ten a.m.
[7] This refined method of tossing up was arranged as follows:
—A certain letter (say the first of the third line) was fixed on, and
each boy turned over a page in succession; he who turned over
that one in which the corresponding letter was nearest to A, won;
and vice versâ.
[8] See “Ups and Downs of a Public School.”
[9] See the full-page illustration of a “Hot,” drawn by Mr R.
Holmes.
[10] I believe the word “continent” is derived from the Latin
contineo, to keep in.
[11] See “Ups and Downs of a Public School.”
[12] This must not be confounded with the Roll which was
published every November, giving a list of the entire
establishment of the College, commencing with the Warden,
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like