How do I make a list of references to a variable?

Hi there, I have an attributes script for each of my charactors, which contains a selection of attribute floats for that character, and a list containing all the affor mentioned floats. This looks something like this:

public class Attributes : MonoBehaviour {

	public List<float> attributesList = new List<float> ();

	public float attribute01;
	public float attribute02;
	public float attribute03;

	void Start () {

		attributesList.Add (attribute01);
		attributesList.Add (attribute02);
		attributesList.Add (attribute03);
	}
}

The rest of my scripts often reference these attributes directly via the floats rather than the list (which is the way I want to keep it if I can to stop the code getting too messy) but there is one infrequent circumstance in which I need to randomly select an attribute via the list to alter it.

I have been trying it out as is, and currently altering a list entry doesn’t alter the corresponding float, so it is clear that the list currently isn’t a list of refernces. After some research through unity and c# documentation, I have tried inserting the word “ref” in places to see if that will help, but unity doesn’t like what I have tried so far.

How then, can I build the list from references rather than the corresponding floats? As I said earlier, I would rather avoid having to use only the list throughout the game.

Thanks in advance,

Pete

If you need float values as references encapsulate them in a class.

    public class FloatRef
    {
        public float Value { get; set; }

        public FloatRef(float value)
        {
            Value = value;
        }
    }

Usage:

            List<FloatRef> floatList = new List<FloatRef>();

            FloatRef one = new FloatRef(1);
            FloatRef two = new FloatRef(2);

            floatList.Add(one);
            floatList.Add(one);
            floatList.Add(two);

Ok, I finally solved this. What I did in the end was create a list of strings containing nothing more than the names of each of my attributes. For the most part, I just access the attributes directly in my code. Then for my small special section of code that needs to choose one attribute at random, I pick an entry from the list of names. Then I use the name of the attribute to locate it, get it and set it as needed. The code needed to use the string to do all this looks complicated and slightly messy, but seeing as it is only needed for a short section of code, I’m not too fussed by this. I can’t speak however for how performace friendly this technique is. Below is a link to the forum post that gave me this information:

http://forum.unity3d.com/threads/access-variable-by-string-name.42487/

I think this forum post gives plenty of info on the subject, however if anyone is reading this and has any questions on how I implemented this, please ask. Hope this helps someone in the future! And thank you again for all the valuable input.

Pete