7 minutes read
C# and Coding on Unity

C# and Coding on Unity

The Unity game engine has quickly become one of the most popular and widely used tools for mobile game developers, especially for those developed for Android devices. Similar to other popular game engines like Unreal, the Unity game engine offers an ever-growing array of tools and features to make game development easier, as well as an intuitive user interface design that lets even relatively new developers build robust applications quickly and easily.

Part of the appeal of Unity is due to the C# programming language that makes up the foundation of the engine - often considered one of the easiest programming languages for new developers to learn, the simplicity of C# has helped to make Unity an exciting option for new programmers to flex their creative muscle. 

For programmers who have begun their coding careers or taken online coding classes to learn the basics of C#, learning how to master the Unity engine combines a number of existing skills and C# principles with some Unity-specific tools and knowledge. In this guide, let’s take a deeper dive into what you need to know about C# and coding on Unity, as well as answer some of the more frequently asked questions about using the language to get the most out of the Unity engine.

What should you know about Unity?

Unity is a 2D/3D engine and framework that provides developers with a system for designing game or app scenes for 2D, 2.5D and 3D. Beyond simply mobile games, Unity has also begun to be used for building training simulators, first-responder applications, and other business-facing applications developed with Unity that need to interface with 2D/3D space. Unity further offers the ability to interact with these applications via not only code, but visual components as well. Additionally, you can export them to every major mobile platform and a whole lot more—for free (there is also a pro/paid version, but the free version offers tons of potential by itself).

Unity supports all major 3D applications and many audio formats, and even understands the Photoshop .psd format so you can just drop a .psd file into a Unity project. Unity allows you to import and assemble assets, write code to interact with your objects, create or import animations for use with an advanced animation system, and much more.

Additionally, one of the most celebrated aspects of Unity is the Unity Asset Store, which many consider the best asset marketplace available anywhere in the gaming market. The Unity Asset Store provides an ever-expanding marketplace of gaming component needs, including:

  • Artwork 
  • 3D models and animation files for 3D models
  • Audio effects and full tracks
  • Plug-ins that can provide a range of developer features and tools
  • Visual scripting systems such as PlayMaker and Behave
  • Advanced shaders, textures, particle effects, and more.

The Unity interface is fully scriptable, allowing many third-party plug-ins to integrate right into the Unity GUI. Most, if not all, professional game developers use a number of packages from the asset store, and if you have something decent to offer, you can publish it there as well. 

C and Coding on Unity

Basics of Coding on Unity: Scenes 

Everything that runs in any game you build exists within a scene. Inside any game that is packaged for a particular platform is a collection of one or more scenes, plus any platform-­dependent code added as needed. You can also include as many scenes as you want in a project. 

An easier way to think of a scene is as a level in a game (although you can have multiple levels in a single scene file simply by moving the player/camera to different points in the scene). This means that when you download third-party packages or potentially sample games from the asset store, you generally must look for the scene files in your project to open. A scene file is a single file that includes all sorts of metadata around the resources used in the project for the current scene, including its properties. As a default, Unity opens the last scene you’ve been working on, although sometimes when Unity opens a project it creates a new empty scene and you’ll have to go find a specific scene using the project explorer. 

Basics of Coding on Unity: GameObjects and Components

GameObjects

Virtually everything in a given scene is what is known as a GameObject, the base class for all objects in whatever Unity scene you work with. A GameObject has a Name, a Tag, a Layer and the Transform (which refers to the position, rotation and scale of any GameObject). Unity uses the left-hand coordinate system, which organizes the coordinates of your computer screen as X (horizontal), Y (vertical) and Z (depth, as in coming in or going out of the screen).

Components 

Components are the means for adding functionality to your GameObjects. Whenever you add something, it can be considered a Component and subsequently appears in the Inspector window. There are MeshRender and SpriteRender Components; Components for camera functionality and audio tools; physics-related Components for actual game operations, path-finding and particle systems, third-party custom Components, and plenty of others. 

Meanwhile, a script Component can be used to assign code while writing with C# or another language to a given object. Components are what bring your GameObjects to life by adding functionality and customization, the elements that help a mobile game stand out from other similar applications.

Basics of Coding on Unity - Scripting 

Scripting is the essential element for telling GameObjects how to behave. To put it simply, it’s the scripts and components attached to the GameObjects, and the ways that they interact with each other, creates the actual gameplay in any project you build. To be clear, scripting in Unity is different from pure programming. For anyone who has more experience with pure programming, the main difference to remember is that with Unity you don’t need to create the code that runs the application, because Unity does all of the work for you. Instead, the gameplay is designed and detailed in your scripts.

Unity runs in a big loop, reading all of the data that is housed within a given game scene. To give a simple example, Unity reads through things like the lights, the meshes, as well as what the behaviors are, and then processes all of this information for you. You give directions to Unity with the instructions that you write in your scripts, and subsequently Unity executes them frame after frame as fast as it can. Achieving a high frame rate is important, because not only will your game look more fluid, but your scripts will also be executed more often, which then makes the actual controls more responsive.

Where else does C# come into play? 

Every language that Unity operates with belongs to the class of languages known as object-oriented scripting languages. Similar to any programming language, scripting languages have syntax (also known as parts of speech). The essential parts of speech are called variables, functions, and classes. 

C# is the primary language that drives the engine, but here are some of the specific elements that actually provide the functionality needed to make the games operate:

  • Variables hold values and references to objects (for example, you can view objects as “bigger” variables). You can think of this almost like a toolkit, holding important elements for use when needed. Variables always begin with a lowercase letter.
  • Functions are collections of code that are used to compare and manipulate these variables. Unlike variables, Functions start with an uppercase letter. Code is organized in functions so that they can be easily reused multiple times in different parts of the program, saving you time and avoiding having to do redundant programming.
  • Classes are a method for structuring code, in order to wrap collections of variables and functions together to create a template that defines the properties of an object.

Scripting is primarily composed of comparing these objects and their current states and values. This is based on logic determining an outcome or resolution.

Learn how to make the most of Unity by mastering C# today!