Assigning a function with a parameter to buttons in for loop with a temp variable

Hello, I am trying to assign a function with a parameter to buttons that I create in for loop. I use a delegate to assign it and I make sure to pass a local temp variable as a parameter.
For some when I click the button it still calls the function with the reference to the last value of tempint.
I have no idea why as I saw countless posts about people using the same thing and it works for them.

    void Start ()
    {
        float activeArea = Screen.height * 0.75f ;
        print(activeArea);
        float buttonDistances = (activeArea) / AudioStreamer.Instance.numberOfStates;
        float currentdist = (activeArea * 0.5f) - buttonDistances;
        float buttonheight = buttonDistances * 0.8f;

        for (int i = 0; i < AudioStreamer.Instance.numberOfStates; i++)
        {
            GameObject newbutton = Instantiate(button);
            newbutton.transform.SetParent(gameObject.transform, false);
            newbutton.transform.localPosition = new Vector3(0, currentdist, 0);

            Button mybutton = newbutton.GetComponent<Button>();
            mybutton.image.rectTransform.sizeDelta = new Vector2(400, buttonheight);

            int tempint = i;
            mybutton.onClick.AddListener(delegate {  CmdSetMusicTo(tempint); }); //<----- This is not working proeprly

            Text mytext = newbutton.GetComponentInChildren<Text>();
            mytext.text = MusicFromManager.Instance.l_from_MusicContainers*.stateName.text;*

currentdist -= buttonDistances;
l_control_Buttons.Add(mybutton);
}

public void CmdSetMusicTo(int statenum)
{
AudioStreamer.Instance.func_SetMusicState(statenum);
}
Help is much appricated, thanks

Found the problem, it was passing the correct number but the function inside the Audio Streamer was not handling it correctly. Was looking in the wrong Class.