Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Schema `OnChange` delegates not removable

See original GitHub issue

Colyseus Unity client version: 0.14.7 (latest) Colyseus server version: 0.14.1 (not latest, but issue is client-side)

If you store a reference to a Schema object other than the root schema, later (after a schema patch?) attempting to remove a delegate from that reference’s OnChange event will fail silently.

See comments in the example client code below for more specifics on what’s going wrong. It looks like Colyseus is doing something to the OnChange references that’s breaking the ability to remove delegates.

Example server code:

export default class Phase extends Schema {
  public value: number;

  constructor() {
    this.value = 1;

export default class GameState extends Schema {
  public readonly phase: Phase;

  constructor() {
    this.phase = new Phase();

export default class GameRoom extends Room<GameState> {

  // ...

  onCreate(options: any) {
    let gameState = new GameState();

    this.clock.setInterval(() => {
      console.log('changing phase');
    }, 1000);


Example client code:

class MyClass {
  private GameState gameState;
  private Phase phase;

  // Start by calling this only once.
  private void AddDelegate(GameState gameState) {
    this.gameState = gameState;
    this.phase = gameState.phase;

    this.gameState.phase.OnChange += MyCallback1;
    this.phase.OnChange += MyCallback2;

  private void MyCallback1(List<DataChange> change) {
    Debug.Log("MyCallback1 called");

    // This unregisters the callback correctly.
    this.gameState.phase.OnChange -= MyCallback1;

  private void MyCallback2(List<DataChange> change) {
    Debug.Log("MyCallback2 called");

    // This doesn't unregister the callback correctly. No error is thrown, and this callback keeps
    // getting called.
    this.phase.OnChange -= MyCallback2;

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

endelcommented, Aug 2, 2021

Interesting, thanks for reporting again @rnd256, I’m gonna have a look at this during this week!

RaidelRosscommented, Aug 22, 2021

I already found the solution, your comments were helpful, thank you

private void ClientAdd(string key, DataClientLobby data)
  var clientLobby = Instantiate(clientLobby_prefab);
  clientLobby.Init(data, key.Equals(lobby.SessionId));
  entities.Add(data, clientLobby);
  data.OnChange += OnChange;
Read more comments on GitHub >

github_iconTop Results From Across the Web

C# Delegation is not being removed
Here's what to do: Step 1: Change your event handler to take an instance of the picture box and not the index to...
Read more >
How to manage different delegate signatures/Types ...
With a Dictionary<Type, List<object>>, i can store by delegate Type, and store the instance as an object. This messes up OOP which i...
Read more >
Event delegation
Hide messages with delegation​​ There's a list of messages with removal buttons [x] . Make the buttons work.
Read more >
Clean event handler invocation with C# 6
It does this by showing that a tight loop that reads such a field will terminate when the field is changed by another...
Read more >
C# static code analysis: Anonymous delegates should not ...
Anonymous delegates should not be used to unsubscribe from Events ... This happens because to remove the entry from the subscription list, a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found