Introduction to Concordance
Values with several writers
A value breaks when several scripts write it and each one assumes it's the only writer. Each script is correct on its own, and the bug appears only when they meet:
- A damage flash restores the sprite color it cached, and erases the poison tint applied during the flash.
- A stun enables movement again while a cutscene still needs it disabled.
- A script destroyed in the middle of its effect never restores the value.
Concordance fixes this for any field, property or setter method, of your scripts or of Unity's, such as
AudioListener.volume, Rigidbody2D.gravityScale or GameObject.SetActive.
Tracking
The Concordance window shows, for each member, every write to it in your project. In Play mode, it flags conflicting writes, two objects overwriting each other's value, which is the bug of a value with several writers, and shows how the value is computed. A member you watch also records its history: who changed it, when, and from which line. For more information, refer to Inspect members.
Modifiers
A script that changes a value for a while writes a modifier: a change layered on top of the value.
To write one, put (Modifier) before the assigned value, and assign Modifier.Stop when the
effect ends:
sprite.color = (Modifier)Color.Lerp(sprite.color, Color.red, 0.5f); // tints the sprite halfway to red
sprite.color = Modifier.Stop; // removes the tint
Concordance keeps the base value, the value without modifiers, and applies the active modifiers to it. Each script removes only its own modifier, and a modifier also ends when the object that wrote it is destroyed.
Code that doesn't use Concordance keeps working: a plain write, an assignment without (Modifier), sets the base
value, and the modifiers apply on top. For more information, refer to
Write modifiers.
Modifier order
When several modifiers change one member, their order can matter: a stun that sets the speed to 0 must apply
after a speed boost. A phase attribute in code, such as [ModifierPhase.Late], gives a script's modifiers
their place among five phases, applied in this order: Earliest, Early, Default,
Late, then Latest. The Concordance window orders the scripts that share a phase. For more information, refer to Order modifiers.
Change events on any member
Most values have no change event. Observe gives any member one, and its handler runs after each
change, from a modifier or a plain write:
AudioListener.volume.Observe(OnVolumeChanged);
For more information, refer to Observe changes.
Metaprogramming
Concordance is metaprogramming: after Unity compiles your scripts, it rewrites them. You keep writing ordinary C#
on the members you already have: speed stays a float, Time.timeScale stays
Unity's property, and no wrapper type replaces them.
A line such as speed *= (Modifier)0.5f or speed.Observe(OnSpeedChanged) compiles as
plain C#, and Concordance turns it into code that knows which member it changes. That's why these lines work on a
field, a property or a setter method, and not on a local variable. Your source files never change, and a line
Concordance can't rewrite is reported in your code editor as you type, with its fix.
For more information, refer to How Concordance works.