Unable to retrieve public property from custom class (C#)

Howdy!

It's better if I show some script to explain this.

This is what my custom class looks like:


public class ButtonData : Object {
    public Rect pos;
    public string label;

    public ButtonData(Rect p_pos, string p_label) {
        pos = p_pos;
        label = p_label;
    }
}

In another class(in the same script component), I've created this monstrosity:


private ArrayList buttons = new ArrayList();

    void Start() {
        buttons.Add(new ButtonData(new Rect(100,100,Screen.width-200,40),"Start Game"));
        buttons.Add(new ButtonData(new Rect(100,160,Screen.width-200,40),"Exit Game"));
    }

Afterwards, I'm trying to do this:


print((ButtonData)buttons[0].label);

However, I seem to be getting the following error:

Assets/Custom Assets/Scripts/controller_startscreen.cs(26,46): error CS1061: Type `object' does not contain a definition for`label' and no extension method `label' of type`object' could be found (are you missing a using directive or an assembly reference?)

Didn't I make it clear enough that the object in buttons[0] is of type ButtonData, and not object?

Could use some help :P

-Tijmen

Everything you pull out of an ArrayList is going to be object, and the way you're casting doesn't actually cast the buttondata, it casts the label

What you want to do instead is use a List, which works almost the same as ArrayList but is generically typed

You'll need to add using System.Collections.Generic; for it to work

If you don't want to change it, you can simply cast everything you pull out of the arraylist in this way:

print(((ButtonData)buttons[0]).label);