Write modifiers
The quick syntax writes a modifier as an assignment with (Modifier) before the value. Instead of writing the
value, the line sets a modifier owned by the object that runs it, which lasts until the script stops it or the owner is
destroyed.
Write a modifier
To turn an assignment into a modifier, perform the following steps:
- Add
using Concordance;to the top of your script. - Put
(Modifier)before the assigned value. - When the effect ends, assign
Modifier.Stopto the member.
The following script halves the gravity of each rigidbody in its trigger, and removes its modifier when the rigidbody leaves:
using Concordance;
using UnityEngine;
public class LowGravityZone : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other) => other.attachedRigidbody.gravityScale *= (Modifier)0.5f;
void OnTriggerExit2D(Collider2D other) => other.attachedRigidbody.gravityScale = Modifier.Stop;
}
Running a modifier line again replaces its modifier instead of adding one, so a modifier line in
Update never piles up modifiers.
A compound assignment such as x *= (Modifier)0.5f is the same as x = (Modifier)(x * 0.5f).
Put parentheses around a value that contains an operator: (Modifier)x * 0.5f applies only to
x, and doesn't compile.
Concordance rewrites each modifier line after Unity compiles your script, so (Modifier) works only
where the line names the member it writes: before the whole right side of an assignment to a field or property, or
before the value argument of a setter method. When a line can't be a modifier, your code editor and the Console
window say why and how to fix it. For every
message, refer to Code analysis reference.
Members that take modifiers
Any field or property with a setter takes modifiers, of your own scripts or of Unity's, whatever its type: a
number, a bool, an enum, a Color, a struct of your own, or a reference such as a
Material. One field or property of a struct, such as offset.x, takes
modifiers too.
An array element, a list element and a dictionary entry take no modifiers. For items in a list, put the field on the
item: enemies[i].speed takes modifiers, enemies[i] doesn't. For a value without a field of
its own, refer to Wrap a value in a property.
A member the engine computes from its own previous value, such as a rigidbody's velocity, takes no modifiers. For more information, refer to Members the engine updates.
Getter and setter methods
Values that Unity exposes as a pair of methods, such as GameObject.SetActive or
Transform.SetSiblingIndex, take modifiers too. Put (Modifier) before the argument:
go.SetActive((Modifier)false);
go.SetActive(Modifier.Stop);
A method that takes a key before the value, such as Material.SetFloat(name, value) or
Animator.SetFloat(name, value), takes no modifiers. Wrap it in a property instead.
Wrap a value in a property
To modify a value that isn't a member, such as a material property, an animator parameter, an audio mixer parameter or a list element, wrap it in a property of your own and modify the property:
public class Flash : MonoBehaviour
{
static readonly int FlashId = Shader.PropertyToID("_Flash");
[SerializeField] Renderer target;
public float Amount
{
get => target.material.GetFloat(FlashId);
set => target.material.SetFloat(FlashId, value);
}
}
flash.Amount = (Modifier)1f; // in any other script
The property is a member like any other: its modifiers combine, it shows in the Concordance window under its own
name, and observers hear its changes. Write the value through the property everywhere. A direct call to
SetFloat elsewhere isn't tracked, and the next combine of the property overwrites it.
The value before the modifier
After (Modifier), the member you write reads as its value before this modifier: the base value, with the earlier
modifiers applied. Concordance computes the line again each time that value changes.
Everything else after (Modifier) is a snapshot, evaluated once, when the line runs:
| Modifier line | Result |
|---|---|
speed = (Modifier)Mathf.Min(speed, maxSpeed) | Caps speed at maxSpeed, as it was when the line ran. |
sprite.color = (Modifier)Color.Lerp(sprite.color, flashColor, flash) | Moves the color toward flashColor. |
canMove = (Modifier)(canMove && hasStamina) | Disables movement if hasStamina was false when the line ran. |
Elsewhere, the member reads as usual, with every modifier applied.
Follow a changing value
A modifier keeps its snapshots until its line runs again. To follow a value that changes over time, write the
modifier from Update:
void Update()
{
t += Time.deltaTime / fadeDuration;
music.volume = (Modifier)Mathf.Lerp(1f, 0.2f, t);
}
When a modifier ends
A modifier ends when one of the following happens:
- Its script assigns
Modifier.Stop, or callsStopModifying. - Its owner, the object that wrote it, is destroyed. Disabling the object keeps its modifiers.
- A
Concordcall stops it, such asConcord.Releasefor a pooled object. - You exit Play mode, or a Test Runner test ends. Concordance then writes each base value back, so a ScriptableObject asset never keeps a value with modifiers in it.
A script destroyed in the middle of its effect therefore never leaves its change behind. To make a modifier outlast the script that writes it, such as a pickup that destroys itself, pass another owner with the regular syntax:
player.speed.Modify(player.speed * 1.5f, owner: player); // lasts as long as the player
Destroy(gameObject);
In a static method, the owner is the class, which is never destroyed: stop the modifier yourself, or pass an owner.
In a static method of a generic class, the owner is the closed type, such as Buff<int>.
An owner that isn't a Unity object ends its modifiers when the garbage collector collects it. A pooled object is
deactivated, not destroyed: refer to Release pooled objects.
InvalidOperationException, since a value with its modifiers would be saved
into the scene or the asset.