Skip to content

A simple way to create variables with global access using Scriptable Objects

Notifications You must be signed in to change notification settings

LuizThiago/GlobalVariables

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Global Variables

A simple way to create variables with global access using Scriptable Objects

Table of Contents
  1. About The Project
  2. Getting Started
  3. Contact

About The Project

The GlobalVariable<T> script allows you to create and manage global variables that can be used anywhere in your Unity project. The variables are stored as ScriptableObjects, and the script provides a mechanism to notify other parts of the code about changes in the variable's value through events. This pattern is useful for managing global states and events that affect the behavior of the game.

(back to top)

Built With

  • C#
  • Unity

back to top

Getting Started

To get a local copy up and running follow these simple example steps.

Installation

  1. Download or fork this project
  2. Move the content to your Unity project (if preferred, only the Scripts folder is necessary)

(back to top)

Usage

Create a new script that inherits from GlobalVariable, where T is the type of the variable. Example:

  1. Create a global float variables script GlobalFloatVariable.cs
    using UnityEngine;
    
    [CreateAssetMenu(menuName = "Global Variables/Float Type", fileName = "GlobalFloatVariable")]
    public class GlobalFloatVariable : GlobalVariable<float> { }
  2. Create an instance of the global variable by right-clicking on the desired folder in the Project tab window and choosing the option (in this example) Create > Global Variables > Global Float Variable
  3. Give your new global variable a name, for example, PlayerHealth
  4. Now, to access it, simply reference the Scriptable Object created in your scripts. Example: TestGlobalFloatVariable.cs
    using System.Collections;
    using UnityEngine;
    
    public class TestGlobalFloatVariable : MonoBehaviour
    {
       [SerializeField] private GlobalFloatVariable _playerHealth; //The reference for the GlobalFloatVariable
    
       private IEnumerator Start()
       {
          //Subscribe to the player health's OnChange event to be notified when the value changes.
          _playerHealth.OnChange.AddListener(OnPlayerHealthChange);
    
          Debug.Log($"[TestGlobalFloatVariable] Player health value: {_playerHealth.Value}");
          string timestamp = System.DateTime.Now.ToString("HH:mm:ss.fff");
          Debug.Log($"[TestGlobalFloatVariable][{timestamp}] Change player health to 100 in 5 second.");
    
          //Wait and set the player health to 100.
          yield return new WaitForSeconds(5);
          _playerHealth.Value = 100;
       }
    
       private void OnPlayerHealthChange(float value)
       {
          string timestamp = System.DateTime.Now.ToString("HH:mm:ss.fff");
          Debug.Log($"[TestGlobalFloatVariable][{timestamp}] Player health changed to {value}");
       }
    }

(back to top)

Contact

LuizThiago - @CodeLuiz - [email protected]

Project Link: https://github.com/LuizThiago/GlobalVariables

(back to top)

About

A simple way to create variables with global access using Scriptable Objects

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages