A script with all variables

In the Realm Rush part of the course, there are a lot of variables that have to be tweaked carefully for the perfect balance. Since it would be tedious to always keep going back and forth in the inspector, i had the idea to make a script that holds all the variables needed.

This is how its working out so far…

using UnityEngine;

public class VariablesScript : MonoBehaviour
{
    [Tooltip("Location: ")]
    [SerializeField] int CostOfPlacingOneTurret = 30;

    [Tooltip("Location: ")]
    [SerializeField] int RewardForKillingTheEnemy = 20;

    [Tooltip("Location: ")]
    [SerializeField] int PenaltyOrAmountEnemyWillSteal = 10;

    [Tooltip("Location: ")]
    [SerializeField] int StartingAmountOfGoldGivenToThePlayer = 150;

    [Tooltip("Location: ")]
    [SerializeField] int MaximumHealthOfAnEnemy = 10;

    [Tooltip("Location: ")]
    [SerializeField] int AmountOfDamageDoneToEnemyDueToCollisions = 10;

    [Tooltip("Location: ")]
    [SerializeField] int HealthIncreasePerEnemyDeath = 10;

    [Tooltip("Location: ")]
    [SerializeField][Range(0f, 5f)] int SpeedOfTheEnemy = 1;

    [Tooltip("Location: ")]
    [SerializeField] float TimeItTakesForNextEnemyToSpawn = 3;

    [Tooltip("Location: ")]
    [SerializeField] int RangeOfTheTurrent;


    Turrent turrent;
    Enemy enemy;
    BankScript bank;
    EnemyHealth enemyHealth;
    enemyScript enemyScript;
    EnemySpawner enemySpawner;

    private void Awake()
    {
        turrent = GetComponent<Turrent>();
        enemy = GetComponent<Enemy>();
        bank = GetComponent<BankScript>();
        enemyScript = GetComponent<enemyScript>();
        enemySpawner = GetComponent<EnemySpawner>();
        enemyHealth = GetComponent<EnemyHealth>();
    }

    private void Update()
    {

    }
}

Now i would need to make every variable PUBLIC so that i can access these in the script. But i feel like there is a better way to do this instead of doing such a tedious process.

Later on, i also plan to make a difficultyScript that dictates and UPDATES some variables like enemy speed, hitpoints etc so that i can adjust the difficulty as the game goes on. So please give me some suggestions about that as well.

The only reason i want to do all this is because i want to keep the code clean and restrict the number of actions one script is performing and make it easy for testing the game.

Yes. The way the course is doing it is the better way. Each variable is part of the responsibility of the component that use them, for example; MaximumHealthOfAnEnemy should be on the Health component because managing the health is the Health component’s responsibility. Enemies may have different maximum health values, turrets may have different costs, some object may not even have the component you are referencing in this script so now you need a new one.

That being said, I often have a script - ScriptableObject to be exact - that holds more global values. It’s just a singleton that lives in a Resources folder where I can add these global variables and access them from anywhere

[CreateAssetMenu(fileName = "GlobalSettings", menuName = "New Global Settings")]
public class GlobalSettingsSO : ScriptableObject
{
    private static GlobalSettingsSO _instance;
    public static GlobalSettingsSO Instance
    {
        get
        {
            if (_instance != null) return _instance;
            _instance = Resources.Load<GlobalSettingsSO>("GlobalSettings");
            return _instance;
        }
    }

    // All the global settings go here
    [field: Header("Player")]
    [field: SerializeField] public int StartingGold { get; private set; }
    // ... more settings here ...
}

Now you right-click in a “Resources” folder (it’s important), Create -> New Global Settings, leave its name “GlobalSettings” (it’s important), and set the values in the inspector. These values can be accessed like any other singleton

int _playerGold = GlobalSettingsSO.Instance.StartingGold;

You can only have one of these (unless you write a bunch that have different filenames, etc. but I do not recommend) because it’s meant to be global settings for the game. It does not change at runtime (nor should you try because it will not be saved)

That’s a good sentiment, but this is not the way. Write the code so components do one thing. That’s the whole beauty of the component system Unity uses.

Thank you Bix for such a great explanation!

If i want, i could make a few variables GLOBAL. But its better to keep the variables where they are. Each script has its own purpose and the related variables must always be on that script.

1 Like

If i want to adjust the difficulty using a script, i could just refer to the variables and tweak them as the game goes on.

Yes. The RPG course has an example of how difficulty changes as enemies get stronger. Higher level enemies have higher max health, etc. This is all done on a lookup table where the enemies can find their max health (among other things) based on their level. Or maybe I just remember it wrong. But there is a LUT for finding changes to stats based on levels

nah you’re fine. Yes, that’s how it’s handled there

Thank you!!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms