【VRChat】Variables and Data Types in UdonSharp: Basics of int, float, and GameObject

Created: 2025-12-19

The fundamentals of UdonSharp programming: variables and data types. From basic types (int, float, bool, string) to Unity types (Vector3, GameObject, Transform) explained with examples.

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.

Concept diagram of variables and data types

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 public and private are 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 is private if 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 like 1, 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 like 3.14, 0.5, -9.81. Values must end with f. 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: true or false. 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 as public, 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 like new 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 TypeDescriptionUsage Example
intIntegerint score = 10;
floatDecimal numberfloat speed = 1.5f;
boolBoolean (true/false)bool isActive = true;
stringStringstring name = "Taro";
GameObjectObject in the scenepublic GameObject doorObject;
TransformObject's position, rotation, scalepublic Transform playerSpawn;
Vector33D coordinates or directionVector3 pos = new Vector3(0, 1, 0);
ColorColor (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.

  1. Create an UdonSharp script and declare public variables.
  2. Attach that script to an object in the scene.
  3. When you select the object in Hierarchy, the declared public variables 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 public are displayed in Unity Editor's Inspector window, allowing value adjustments and object assignments.
  • Variables declared as private are 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.