C Sharp

Unity: How to Exit a Game on Button Click

Updated: September 29, 2021 By: Pennywise

In Unity, you can programmatically quit a game in Unity by calling the Application.Quit() method. The code snippet below demonstrates how to exit a game when a button gets clicked: Note that your game will NOT…

Unity: DisplayDialogComplex Example

Updated: February 12, 2023 By: Pennywise

This practical article walks you through a complete example that demonstrates how to implement a complex dialog in Unity by using the built-in EditorUtility.DisplayDialogComplex static function. Without any further ado, let’s get our hands dirty. A…

C#: How to Convert a Numeric String to Int/Double

Updated: February 12, 2023 By: Pennywise

This article walks you through a few examples of converting a numeric string to an integer or a double in C#. Converting Numeric Strings to Integers Using Parse and TryParse Methods You can use the following…

Unity – How to Show a Confirmation Dialog

Updated: September 23, 2021 By: A Goodman

In order to show a confirmation dialog in Unity, you can use the EditorUtility.DisplayDialog() method. Before calling it, you need to add: Example The code: Screenshot: If you hit the “Yes” button, you will see: If…

Unity – How to Run a Function after a Delay

Updated: September 23, 2021 By: A Goodman

In order to execute a function after a delay in Unity, you can use Invoke(). If you want to invoke a function after a delay, then repeatedly call it, use InvokeRepeating(). Example: Output: Happy coding!

Unity – Programmatically Enable/Disable a Script Component

Updated: September 23, 2021 By: Pennywise

This short and straight-to-the-post post shows you how to programmatically enable or disable of C# file in Unity (not using the checkbox in the Inspector panel). Let’s say we have a C# file named Rocket that…

Unity – Programmatically Count the Scenes in Build Settings

Updated: September 22, 2021 By: Pennywise

In Unity, you can count the scenes that were added to Build Settings by using the following C# code: Example The function below will load the next scene when the index of the next scene is…

Unity – Light Becomes Darker when Load/Reload Scenes

Updated: September 22, 2021 By: A Goodman

When loading a new scene or reloading the current scene in Unity with the SceneManager.LoadScene() method, I notice an unexpected behavior: the environment and everything get darkened. That is not what I want so I try…

Unity – How to Change the Default Gravity

Updated: September 21, 2021 By: Pennywise

The steps listed below show you how to set your custom values for gravity in Unity (there is no difference between Windows and Mac). 1. Navigate to “Edit” > “Project Settings…” by using your mouse. 2….

C#: 2 Ways to Check if a String contains another String

Updated: September 20, 2021 By: Napoleon

This short and straight-to-the-point article shows you two different ways to whether a string contains another string in C Sharp. The first approach is case SENSITIVE while the second one is case INSENSITIVE. Using the Contains()…

Unity: Enable/Disable Gravity from C# Script

Updated: September 18, 2021 By: A Goodman

In Unity, you can programmatically enable or disable gravity for an object from your C# script as below: Example The following code will turn on gravity for an object 3 seconds after the game starts: You…

C#: Mixing Variables with String Literals

Updated: September 20, 2021 By: Napoleon

The three examples below show you how to use variables within string literals in C#. 1. Using the $ symbol Output: 2. Using + Operation Output: 3. Using String.Format Output: Happy coding!

1 2