You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

How to define a constant in Unity using C#

How to define a constant in Unity using C#

Ben Liu
Ben Liu

Constants are "constantly" used in the Unity projects, they are working like the global config settings. Some features of the constants:

  • Define only once
  • Can't be changed
// define a constant variable in a class
public class Constants {
      public const int LIVES = 3;
}


// use the constant
public class Player : MonoBehaviour
{
    int lives;
 
    void Awake()
    {
        lives = Constants.LIVES;
    }
}

Ben Liu

Please feel free to contact me if you have any question. <br> https://t.me/NeedoneApp