Menu
CoddyTech

Shared Settings

Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 21 of 50.

A difficulty setting changes every enemy at once, so it fits one asset that all of them point at. Each enemy reads the asset's speed every frame, and the menu's Hard button writes into the same asset:

[CreateAssetMenu(menuName = "Game/Difficulty")]
public class Difficulty : ScriptableObject
{
    public float enemySpeed = 2f;
}
public class Enemy : MonoBehaviour
{
    public Difficulty difficulty;
    public Transform player;

    void Update()
    {
        float step = difficulty.enemySpeed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, player.position, step);
    }
}
// DifficultyMenu.cs, wired to the Hard button's onClick
public Difficulty difficulty;

public void SetHard()
{
    difficulty.enemySpeed = 4f;
}

The enemies' fields and the menu's field hold the one asset, so after SetHard() the next Update of every enemy reads 4, enemies spawned later from a prefab with the asset in their field included. That works because each enemy reads difficulty.enemySpeed when it moves. An enemy that copied it into a field of its own in Start keeps moving at 2.

Where a change made during the game ends up depends on where the game runs. A build loads its assets as they were built every time it starts, so the player's Hard is gone in the next game: to keep a choice, save it with PlayerPrefs and write it back into the asset when the game starts. In the Editor, the asset in your project is the one the game changes, and the change stays after you press Stop, unlike the scene's objects, which go back to their Inspector values. The next Play starts on Hard, and the asset keeps 4 until you set it back.

challenge icon

Challenge

Medium

Three sailboats cross the bay, pushed by the wind, and the Up and Down arrows turn the wind up and down between 0 and 5. But each boat has its own windStrength field, and the WindVane changes only its own strength, so the arrows change nothing on the water. Share one asset instead. WindSettings.cs is ready, and the project's Breeze asset (strength 2) is in the wind field of every boat and of the WindVane.

  • Sailboat: replace windStrength with a public field wind of type WindSettings, and sail at wind.strength.
  • WindVane: replace strength with a public field wind, and make the arrows change wind.strength, so every boat feels it. The text shows it, as Wind: 3.
  • A build loads the Breeze asset fresh each time it starts, which would lose the player's wind. Save wind.strength under the PlayerPrefs key Wind each time it changes, and in Start put a saved value back into the asset. A new player keeps the asset's 2.

Press Play, change the wind, then Stop and Play again, then Check.

Try it yourself

using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;

public class WindVane : MonoBehaviour
{
    public TMP_Text windText;
    public float strength = 2f;

    void Start()
    {
        Show();
    }

    void Update()
    {
        if (Keyboard.current.upArrowKey.wasPressedThisFrame)
        {
            strength = Mathf.Min(strength + 1f, 5f);
            Show();
        }
        if (Keyboard.current.downArrowKey.wasPressedThisFrame)
        {
            strength = Mathf.Max(strength - 1f, 0f);
            Show();
        }
    }

    void Show()
    {
        windText.text = "Wind: " + strength;
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Game Architecture in Unity