How to get the name of a toggle from within its callback function ?

Hi,

I am writing a bunch of toggle callbacks which have the same behavior.
Instead of writing 20 callback functions, I would like to write one which gets the toggle name and acts appropriately.

I tried the following code, but the transform.name only gives me the name of the top parent object, not the toggle name.

Any ideas how I can get the toggle name from within the callback function ?

public void OnToggleLocataires(bool t)
{
    Debug.Log(" name = " + this.transform.name);

    if (t)
    {
        if (AddCritere(Critere.logLocataires))
        {
            inputFieldLogLocataires.gameObject.SetActive(true);
            audioManager.PlayClickOK();
        }
        else
        {
            audioManager.PlayClickNOK();
        }
    }
    else
    {
        RemoveCritere(Critere.logLocataires);
        inputFieldLogLocataires.gameObject.SetActive(false);
    }
}

The word ‘this’ refers to the script. Script.transform refers to the transform that the script (or class) resides in. If you want information about the object of the toggle, you should probably be passing the object as an argument for your callback function. Since this callback function only exists in this class, it will only ever return that one object for the keyword ‘this.’ you could also try using instanced classes rather than a master class, where the instanced class is added as a component to your toggle. Depends on how you want it to function.

I have a couple questions to get clarity. When you refer to a Toggle’s callback function are you talking about Unity’s Toggle script and its OnValueChanged event? If so I think the issue may be what you’ve attached your script to. Is the script you created that holds your public void OnToggleLocataires(bool t) located on the toggle or the parent object? If its located on the parent that’s probably why “this.transform.name” returns the parent name.