---
title: "Statics"
content_type: tutorial
url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l?language=en"
language: en
container_type: course
container_title: "Intermediate Scripting"
container_url: "https://learn.unity.com/course/intermediate-gameplay-scripting.md?language=en"
standalone_markdown_url: "https://learn.unity.com/tutorial/statics-l.md?language=en"
previous_title: "Ternary Operator"
previous_url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/ternary-operator?language=en"
previous_markdown_url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/ternary-operator.md?language=en"
next_title: "Method Overloading "
next_url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/method-overloading?language=en"
next_markdown_url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/method-overloading.md?language=en"
difficulty: intermediate
duration_minutes: 5
unity_version:
  - "2019.3"
  - "2019.2"
  - "2019.1"
  - "2018.4"
  - "2018.3"
  - "2018.2"
  - "2018.1"
  - "2017.4"
  - "2017.3"
  - "2017.2"
  - "2017.1"
  - "5.x"
  - "4.x"
supported_unity_versions:
  - name: "2019.3"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=2019.3"
  - name: "2019.2"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=2019.2"
  - name: "2019.1"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=2019.1"
  - name: "2018.4"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=2018.4"
  - name: "2018.3"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=2018.3"
  - name: "2018.2"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=2018.2"
  - name: "2018.1"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=2018.1"
  - name: "2017.4"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=2017.4"
  - name: "2017.3"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=2017.3"
  - name: "2017.2"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=2017.2"
  - name: "2017.1"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=2017.1"
  - name: "5.x"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=5.x"
  - name: "4.x"
    url: "https://learn.unity.com/course/intermediate-gameplay-scripting/tutorial/statics-l.md?language=en&version=4.x"
last_updated: "2026-08-04T14:24:52.000Z"
---

# Statics

Learn to declare and use static variables, methods, and classes in your Unity scripts.

Learn how to create static variables, methods, and classes.

## 1. Statics

[Video](https://connect-mediagw.unity.com/h1/20220110/videos/3cb8d1f9-2810-4275-aef0-ef68b71bd502_U_Intermediate_3_Statics.mp4) ([Transcript (en)](https://connect-mediagw.unity.com/h1/20220111/4708ecf9-221f-4a25-bb32-d1ab29bf966d_I_03_Statics_V6.0.vtt))

**Enemy**

```csharp
using UnityEngine;
using System.Collections;

public class Enemy
{
    //Static variables are shared across all instances
    //of a class.
    public static int enemyCount = 0;

    public Enemy()
    {
        //Increment the static variable to know how many
        //objects of this class have been created.
        enemyCount++;
    }
}
```

**Game**

```csharp
using UnityEngine;
using System.Collections;

public class Game
{
    void Start () 
    {
        Enemy enemy1 = new Enemy();
        Enemy enemy2 = new Enemy();
        Enemy enemy3 = new Enemy();

        //You can access a static variable by using the class name
        //and the dot operator.
        int x = Enemy.enemyCount;
    }
}
```

**Player**

```csharp
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour 
{
    //Static variables are shared across all instances
    //of a class. 
    public static int playerCount = 0;

    void Start()
    {
        //Increment the static variable to know how many
         //objects of this class have been created.
        playerCount++;
    }
}
```

**PlayerManager**

```csharp
using UnityEngine;
using System.Collections;

public class PlayerManager : MonoBehaviour 
{
    void Start()
    {
        //You can access a static variable by using the class name
        //and the dot operator.
        int x = Player.playerCount;
    }
}
```

**Utilities**

```csharp
using UnityEngine;
using System.Collections;

public static class Utilities 
{
    //A static method can be invoked without an object
    //of a class. Note that static methods cannot access
    //non-static member variables.
    public static int Add(int num1, int num2)
    {
        return num1 + num2;
    }
}
```

**UtilitiesExample**

```csharp
using UnityEngine;
using System.Collections;

public class UtilitiesExample : MonoBehaviour 
{
    void Start()
    {
        //You can access a static method by using the class name
        //and the dot operator.
        int x = Utilities.Add (5, 6);
    }
}
```
