How do I Add value to a list every time you click a button. Then display list in text box?

public void onClick()
    {
        List<Studio> beatStorage = new List<Studio>();
        beatmade++;
        beatStorage.Add(new Studio("Beat" + beatmade, ga.beatScore));
        //beatStorage.Sort();

        foreach(Studio value in beatStorage)
        {
            beatText.transform.SetParent(parentPanel.transform);
            beatText.text = value.beatNames + "\n";
            beatText.color = Color.black;
            beatText.fontSize = 14;
        }

    }

That’s the function that’s called when you click a button. But it only shows one value. What do I do?

You are instantiating a new list each OnClick, declare and instantiate the list outside your onClick method.

Also you’ll want to set the text color, position and fontSize outside of the function. Then t oadd the text to the textbox, use:

beatText.text += value.beatNames + "\n";

And for next time, when posting code always use code tags. That makes is a lot better readable for everyone.

Sorry fixed the code. Also If I take the new list and beatStorage.add lines outside the code I wouldn’t be able to access it in the foreach statement correct?

As long as they are in the same class you can access them as they are in the same scope.

Looks like it’s working. Should I be doing all the text stuff in void start. Then the beatStorage.add in another functions that’s called everytime you click the button?

Something like that. I was just re-reading the code and noticed I missed some things (see how good code tags are? :stuck_out_tongue: ).

I quickly made this:

List<Studio> beatStorage = new List<Studio>();

// Call this for example in Start()
public void SetText()
{
        beatText.transform.SetParent(parentPanel.transform);
        beatText.color = Color.black;
        beatText.fontSize = 14;
}

public void onClick()
{
    beatmade++;
    string beatName = "Beat" + beatmade;
    beatStorage.Add(new Studio(beatName, ga.beatScore));

    // No need to rewrite the entire textbox each time you click a button,
    // you should be able to just add the new beat to the textbox.
    beatText.text += beatName + "\n";
}

Thanks it’s working that’s great. The other problem I have is I want to randomly generate a number and save it to each beat. The variable ga.beatscore gives me an ArgumentException error. I tried moving the randomgenerator if function into the onClick function but that doesn’t work.

if (pm.level == 1)
         {
        float randomnumber = Random.Range(1, 100);
        if (randomnumber <= 3)
        {
            beatRating = 5;
        }
        else if (randomnumber > 3 && randomnumber < 7)
        {
            beatRating = 4;
        }
        else if (randomnumber > 7 && randomnumber < 20)
        {
            beatRating = 3;
        }
        else if (randomnumber > 20 && randomnumber < 50)
        {
            beatRating = 2;
        }
        else if (randomnumber > 50 && randomnumber <= 100)
        {
            beatRating = 1;
        }
         }