Pass a property of a component to another method.

I have an array of prefabs that each have a script attached with a frequency float variable. I want to pass that property from each item in the array into a method that will pick one randomly with more chance for higher frequency items. I can’t seem to get it to work.

Here is what I am trying. Any help is appreciated.

 public class LetterSpawner : MonoBehaviour{
    
        public float xMin;
        public float xMax;
        public GameObject[] letterArray;
    
        // Use this for initialization
        void Start() {
    
        }
    
        public void SubmiToRandomizer()
        {
            PickRandom(letterArray[].GetComponent<Letters>().frequency);
        }
    
    
        public float PickRandom(float[] probs)
        {
            float total = 0;
    
            foreach(float elem in probs)
            {
                total += elem;
            }
    
            float randomPoint = Random.value * total;
    
            for (int i = 0; i < probs.Length; i++)
            {
                if(randomPoint < probs*)*

{
return i;
}
else
{
randomPoint -= probs*;*
}
}
return probs.Length - 1;
}

If you want to turn your list of GameObjects into a list of floats, something like the following should work.

At the very top of your file, add the following if it’s not already there:

using System.Linq;

then in SubmiToRandomizer:

float[] probs = letterArray.Select( x => x.GetComponent < Letters >().frequency ).ToArray();
PickRandom(probs)

I’m not in front of a compiler, so there might be typos, but something like that should work.