Unity C# Trainer

A Game Designed to Teach Beginners C# in a quick interactive way using the Roslyn Compiler.

Visit Itch Page

About The Project

The Unity C# Trainer is a fully interactive learning tool designed to teach beginners the core principles of C# programming inside Unity. Unlike static tutorials or pre-recorded lessons, this project allows players to write real C# code, compile it in-game using Roslyn, and immediately see the results inside a live Unity environment.

The trainer features a complete workflow: a built-in script editor, a real-time console, a dynamic compiler, and a series of fully guided lessons ranging from basic variables to fixing a complete player controller used in a playable demo world.



Main Features


Development Challenges

Performance: Unity by default does not have rudimentary support for Runtime code injection, utilising the Roslyn Compiler allowed for this to be possible but insuring Runtime code injection didn't cause any performance hitches was a huge undertaking.


Replicating Unity Functionality: When the core logic was implemented for Runtime code modification, ensuring certain voids like 'Update' would function like it would if it wasn't a Runtime injection seemed like it would be a huge problem to ensure proper functionality. I later worked out that I could link the user's 'Update' to the Roslyn Controller's 'Update'. Fetching the "User Script's" implementation and invoking it in the Roslyn Controller's implementation allowed for seamless 'Update' functionality as there was only 1 'User Script' running at a time


Lesson Display: In order to properly convey lessons to the player, I had to create a system that injects pre-made code that I wrote into the code editor. That was simple by creating a manager and a 'Lesson' class that holds the 'Block Name', 'Sample Code' and a simple toggle for if there is another lesson called 'DisableNextButton'. Conveying these lessons was made easy by utilising the interactive storytelling asset 'Fungus'. Small modifications to the code were made due to how the lesson manager was designed.


Unity Console Functionality: Utilising Unity's built-in console for error detection was a key part in making this game possible. Even though the code is injected in Runtime, the 'CSharpCompilation' dll that is a requirement for Roslyn. With the limited time I had to produce this project, I had to resort to an asset called 'Quantum Console' by 'QFSW'.


Code Editor: Wanting to replicate an IDE like Visual Studio or Rider lead to being a complicated process with figuring out syntax highlighting and intellisense. With how complicated intellisense is, it wasn't feasible in my given time frame to replicate it within Unity. When developing the compiler for the game, I planned on creating the code editor myself of which I did make progress in but because of the limited time I had, I utilised yet another asset called 'InGame Code Editor' by 'Trivial Interactive'. This allowed me to develop the project more than previously planned as I originally intended only 6 short lessons.


What I Learned

Development

Full Code Available on Request


1. Using Roslyn Inside Unity

The Unity C# Trainer uses the Roslyn compiler to dynamically compile user-written code in real time. This allows beginners to learn C# interactively while seeing their scripts affect objects inside the scene.

Roslyn & Editor Imports

2. Compiling User Code

When the player clicks Compile, their script is parsed into a syntax tree and passed into a Roslyn compilation pipeline. Any previous runtime script instances are safely disposed to avoid memory leaks.

Compile & Cleanup

3. Building the Runtime Assembly

Unity does not allow runtime compilation natively, so the game uses Roslyn to generate an in-memory DLL containing the player’s script. All currently loaded assemblies are included as references so the script can access Unity APIs safely.

Assembly Builder

4. Detecting the User’s Class

Once the in-memory DLL is created, the trainer searches for the first class that extends MonoBehaviour. This becomes the script that will run inside the game world.

Detecting MonoBehaviour

5. Final Challenge Player Spawn

For the final challenge of the trainer, the game spawns a fully interactable character. The user’s compiled script is attached as its controller, teaching how real game logic drives gameplay.

Player Spawn Logic

6. Runtime Update Invocation

If the user writes an Update() method, the trainer invokes it manually, effectively simulating how Unity’s real MonoBehaviour lifecycle works.

Runtime Update Loop

7. Script Injection & Loading

The trainer supports loading scripts from external files, and injecting template code directly into the editor to guide beginners through challenges.

Inject & Load Scripts

8. Lesson Manager

The game requires a manager for the lessons to be loaded into the code editor and the dialogue to be loaded into Fungus.

Lesson Manager

×