Signal 'pressed' is already connected to given callable 'Delegate::Invoke' in that object

Godot Version

4.6

Question

Hello everyone

I try to make a UI scene similar to this one from Wargroove, and for that, I created scenes, including one, called “UnitRecruitOption” :

using System;
using Godot;

public partial class UnitRecruitOption : Container
{
    private Button button;
    private FakeUnit unit; // Name + stats + sprite
    private int cost;
    public event Action<FakeUnit> ChosenUnit; // When the unit is finally chosen. The higher up will know if expensive or not
                                              // Called when the node enters the scene tree for the first time.
    private static readonly string NodeScene = "res://GodotRes/UI/unit_recruit_option.tscn";
    private bool DeployChoice = true;
    public override void _Ready()
    {
        GD.Print("Instance UnitRecruitOption");
        button = GetNode<Button>("UnitButton");
        button.Pressed += CreateUnit; // Equivalent to connect
    }

    public override void _ExitTree()
    {
        button.Pressed -= CreateUnit; // Unsubscribes
    }

    /// <summary>
    /// Broadcast for the entire game that a new unit must be created
    /// </summary>
    private void CreateUnit()
    {
        ChosenUnit?.Invoke(unit);
    }
}

I use built-in C# events, as they are recommended instead of Godot signals when using C#

Is there a solution ? Thank you in advance

Ok, the problem was outside of it, I fixed it, but I’ll explain what I learnt, it can help :

I tried to force the _Ready() because it didn’t printed anything, but it was causing the error
Basically, the signal tried to add itself twice. That is what the error messages means ; you tried to add the same method to the event/signal twice

“But about the ready ? why did you forced it instead of it triggering itself ?”
Because it wasn’t in the grand scheme of the game. It was a single scene, disconnected from the rest, and when I tried to manipulate it further (it manipulated unit, precisely). So when I called it and wanted to manipulate the scene, including button, I had an error about the value

So, before losing a few hours, allow me to explain how to properly do it :

  1. Instanciate your scene
  2. Add it as a child to the node you want
  3. Voilà, the _Ready should trigger itself
  4. Once triggered, you can do every shenaningans you want to your scene