Book Image

Learning C# by Developing Games with Unity 2020 - Fifth Edition

By : Harrison Ferrone
Book Image

Learning C# by Developing Games with Unity 2020 - Fifth Edition

By: Harrison Ferrone

Overview of this book

Over the years, the Learning C# by Developing Games with Unity series has established itself as a popular choice for getting up to speed with C#, a powerful and versatile programming language that can be applied in a wide array of application areas. This book presents a clear path for learning C# programming from the ground up without complex jargon or unclear programming logic, all while building a simple game with Unity. This fifth edition has been updated to introduce modern C# features with the latest version of the Unity game engine, and a new chapter has been added on intermediate collection types. Starting with the basics of software programming and the C# language, you’ll learn the core concepts of programming in C#, including variables, classes, and object-oriented programming. Once you’ve got to grips with C# programming, you’ll enter the world of Unity game development and discover how you can create C# scripts for simple game mechanics. Throughout the book, you’ll gain hands-on experience with programming best practices to help you take your Unity and C# skills to the next level. By the end of this book, you’ll be able to leverage the C# language to build your own real-world Unity game development projects.
Table of Contents (16 chapters)

Time for action  rolling the dice

Let's simulate a tabletop game scenario with a switch statement and fall-through case, where a dice roll determines the outcome of a specific action:

  1. Create an int variable, named diceRoll, and assign it a value of 7.
  2. Declare a switch statement with diceRoll as the match expression.
  3. Add three cases for possible dice rolls: 7, 15, and 20
  4. Case 15 and 20 should have their own debug logs and break statements, while case 7 should fall through to case 15.
  5. Save the file and run it in Unity:

If you want to see the fall-through case in action, try adding a debug log to case 7, but without the break keyword.

With diceRoll set to 7, the switch will match with the first case, which will fall through and execute case 15 because it lacks a code block and a break statement. If you change diceRoll to 15 or 20, the console will show their respective messages, and any other value will fire off the default case at the end of the...