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;
}