Overview
In programming, a variable is like a "box" for temporarily storing data. Variables are essential for handling dynamically changing information such as player health, object positions, and game scores.
Additionally, data types define what kind of data can be stored in a variable. You need to select the appropriate type according to the kind of data—integers, decimal numbers, strings, and so on.
This article explains the major data types available in UdonSharp and the basic methods for declaring and using variables.

Declaring Variables
To use a variable in UdonSharp (C#), you first need to "declare" it. Variable declaration follows this syntax:
access_modifier data_type variable_name;
You can also set an initial value at the time of declaration:
access_modifier data_type variable_name = initial_value;
- Access Modifier: Defines where the variable can be accessed from. Mainly
publicandprivateare used.public: Displayed in Unity Editor's Inspector window, allowing value adjustment. Also accessible from other scripts.private: Used only within that script. Not displayed in the Inspector. The default isprivateif not specified.
- Data Type: Specifies the type of data stored in the variable (described below).
- Variable Name: The name that identifies the variable. It's important to give descriptive names.
using UdonSharp;
using UnityEngine;
public class VariableDeclarationExample : UdonSharpBehaviour
{
// Declaration of a public variable (displayed in Inspector)
public int publicScore = 100;
// Declaration of a private variable (not displayed in Inspector)
private float privateSpeed = 5.5f;
// Declaration without initial value (numeric types are initialized to 0, bool to false, reference types to null)
public GameObject targetObject;
}
Major Data Types
Here are the basic data types commonly used in UdonSharp.
1. Numeric Types
-
int(Integer Type): Stores integers like1,0,-100. Used for scores, counts, and other values where decimal precision is not needed.public int playerCount = 0; -
float(Floating-Point Type): Stores numbers with decimal points like3.14,0.5,-9.81. Values must end withf. Used for object positions, angles, speeds, time, and other continuous values.public float moveSpeed = 3.0f;
2. Boolean Type
-
bool(Boolean Type): Stores only two states:trueorfalse. Used for switch ON/OFF states, condition checks, and similar purposes.public bool isDoorOpen = false;
3. String Type
-
string(String Type): Stores sequences of text like "Hello, World!". Written enclosed in double quotes ("). Used for player names, UI messages, chat content, and similar purposes.public string message = "Welcome!";
4. Unity-Specific Types
In Unity world creation, types provided by the Unity engine are frequently used.
-
GameObject: Represents all objects in the scene (characters, backgrounds, lights, etc.). When declared aspublic, you can drag and drop scene objects from the Inspector.public GameObject playerAvatar; -
Transform: A component that manages an object's Position, Rotation, and Scale. Used when moving or rotating objects.public Transform targetMarker; -
Vector3: Represents 3D vectors (coordinates or directions). Values are set likenew Vector3(x, y, z). Used for specifying object positions and movement directions.// Position vector at (1.0, 2.0, 3.0) private Vector3 spawnPoint = new Vector3(1.0f, 2.0f, 3.0f); -
Color: Stores RGBA (red, green, blue, alpha) color values. Used for light colors and object color changes.public Color lightColor = Color.yellow;
Data Type Reference Table
| Data Type | Description | Usage Example |
|---|---|---|
int | Integer | int score = 10; |
float | Decimal number | float speed = 1.5f; |
bool | Boolean (true/false) | bool isActive = true; |
string | String | string name = "Taro"; |
GameObject | Object in the scene | public GameObject doorObject; |
Transform | Object's position, rotation, scale | public Transform playerSpawn; |
Vector3 | 3D coordinates or direction | Vector3 pos = new Vector3(0, 1, 0); |
Color | Color (RGBA) | Color c = Color.red; |
Using Variables in the Inspector
The biggest advantage of declaring variables as public is that you can directly edit their values in Unity Editor's Inspector window.
- Create an UdonSharp script and declare
publicvariables. - Attach that script to an object in the scene.
- When you select the object in Hierarchy, the declared
publicvariables are displayed in the Udon Behaviour component in the Inspector.
This allows you to fine-tune gimmick behavior without modifying code. For example, you can easily find the optimal speed by adjusting character movement speed (moveSpeed) in the Inspector.
For variables of types that reference objects like GameObject or Transform, you can drag and drop target objects from the Hierarchy or Project window to assign them. This allows you to link scripts with objects in the scene.
Summary
- Variables are named containers for storing data used in programs.
- Data types define the kind of data that can be stored in variables (integers, decimal numbers, strings, etc.).
- Variables are declared in the format
access_modifier data_type variable_name;. - Variables declared as
publicare displayed in Unity Editor's Inspector window, allowing value adjustments and object assignments. - Variables declared as
privateare used only for internal script processing.
Variables and data types are the most fundamental building blocks of programming. A solid understanding of these concepts forms the foundation for creating more complex gimmicks.