© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
10 replies
platinumdalek

❔ Delegate never assigned to (SOLVED!)

I am trying to simplify my code by having a list of delegates, 1 for each player. The game can have up to 4 players. I want to callback only on the player who needs the event. To attempt that I created a switch statement with the players index and then register/invoke events for that player only. However, I am getting a warning that my delegates are never assigned to. What am I doing wrong? Thank you for the help!
Notes:
* RegisterForMovementAction and RaiseMovementEvent are being called.
* "action" is null in both cases. Its only writing to the local variable, not the member field.
public class BombermanPlayerChannel : ScriptableObject {
    public delegate void BombermanPlayerMovementAction(InputAction.CallbackContext context);
  private BombermanPlayerMovementAction OnBombermanPlayerMovementRequested0; // <--- warning on this line saying "Is never assigned to"
    private BombermanPlayerMovementAction OnBombermanPlayerMovementRequested1;

    private BombermanPlayerMovementAction GetMovementActionForPlayer(int player_index) {
        switch (player_index) {
            case 0:
                return OnBombermanPlayerMovementRequested0;
            case 1:
                return OnBombermanPlayerMovementRequested1;
        }
        throw new System.Exception("Unexpected player index!");
    }

  public void RegisterForMovementAction(int player_index, BombermanPlayerMovementAction callback) {
        GetMovementActionForPlayer(player_index) += callback;
        
        // Also tried this:
    BombermanPlayerMovementAction action = GetMovementActionForPlayer(player_index);
    action -= callback;
    }

  public void RaiseMovementEvent(int player_index, InputAction.CallbackContext context) {
    BombermanPlayerMovementAction action = GetMovementActionForPlayer(player_index);
    if (action != null) {
      action.Invoke(context);
    }
  }
public class BombermanPlayerChannel : ScriptableObject {
    public delegate void BombermanPlayerMovementAction(InputAction.CallbackContext context);
  private BombermanPlayerMovementAction OnBombermanPlayerMovementRequested0; // <--- warning on this line saying "Is never assigned to"
    private BombermanPlayerMovementAction OnBombermanPlayerMovementRequested1;

    private BombermanPlayerMovementAction GetMovementActionForPlayer(int player_index) {
        switch (player_index) {
            case 0:
                return OnBombermanPlayerMovementRequested0;
            case 1:
                return OnBombermanPlayerMovementRequested1;
        }
        throw new System.Exception("Unexpected player index!");
    }

  public void RegisterForMovementAction(int player_index, BombermanPlayerMovementAction callback) {
        GetMovementActionForPlayer(player_index) += callback;
        
        // Also tried this:
    BombermanPlayerMovementAction action = GetMovementActionForPlayer(player_index);
    action -= callback;
    }

  public void RaiseMovementEvent(int player_index, InputAction.CallbackContext context) {
    BombermanPlayerMovementAction action = GetMovementActionForPlayer(player_index);
    if (action != null) {
      action.Invoke(context);
    }
  }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Assigning Lambda to Delegate (PurposesReason)
C#CC# / help
4y ago
Delegate add or equal to?
C#CC# / help
4y ago
Delegate type mismatch
C#CC# / help
3y ago