Shared Settings
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 25 of 64.
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
MediumMake the arrows change the wind on every boat by sharing one asset.
- In
Sailboat, replacewindStrengthwithpublic WindSettings wind;and sail atwind.strength. - In
WindVane, replacestrengthwithpublic WindSettings wind;. - Make the arrows change
wind.strength(0 to 5) and the text show it, asWind: 3.
Press Play and press Up: all three boats speed up. 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;
}
}
All lessons in Game Architecture in Unity
1Components, Not Hierarchies
Small ComponentsA Base Enemy ScriptAbstract ComponentsComponents Working TogetherRecap: Modular TurretRecap: Twin Cannon4Serialized Data
Serializable ClassesLists in the InspectorEnums in the InspectorRecap: Loot TableRecap: Roll the Loot2Interfaces in Unity
IDamageableInterfaces and TryGetComponentGetComponentsRecap: Interact KeyRecap: Nearest in Reach5ScriptableObjects
Data AssetsWeapon StatsShared SettingsSave the WindWaves as DataRecap: Item DatabaseRecap: Buy from the ShopPractice on your own: Unity playground