How to get all array float values then do Mathf.Min?
this is the code :
void Update()
{
vencedor = Mathf.Min(/*gets all array float values then select the minimum value*/);
}
does someone know how to do it?
Answer by MacDx · Dec 20, 2017 at 06:05 PM
Mathf.Min can already accept an array of floats and return the smallest one of the array. I don't know where your array comes from so I'll just give you a simple example.
void Update()
{
float[] arrayOfFloats = {1.0f,2.5f,3.0f,4.2f};
vencedor = Mathf.Min( arrayOfFloats);
}
In this case Mathf.Min will return 1.0f and thus 1.0f will be assigned to vencedor, since that's the smallest value in the example array I wrote.
Hope this helps!
i'm sorry i forgot to say not this i want, because the array gets all objects in scene, and i don't want to specify the number of elements in array :
using UnityEngine;
using System.Collections;
public class GameStatistics : $$anonymous$$onoBehaviour {
public float[] tempos;
public GameObject[] carros;
public float vencedor;
void Start()
{
carros = GameObject.FindGameObjectsWithTag("Carro");
tempos = new float[carros.Length];
}
void Update()
{
vencedor = $$anonymous$$athf.$$anonymous$$in(/*gets value of elements in array the select the $$anonymous$$imun value*/);
}
}
i want it to get each value of each element in array then select the $$anonymous$$imum, and i don't know how to automatically get each value
i'm sorry i forgot to say not this i want, because the array gets all objects in scene, and i don't want to specify the number of elements in array
I did not "specify" the length of the array in my example, it could be any array of float created however you want, with whatever arbitrary length, getting values from some objects in the scene or whatever. The point is that $$anonymous$$athf.$$anonymous$$in does not care how you construct the array.
i want it to get each value of each element in array then select the $$anonymous$$imum, and i don't know how to automatically get each value
Like I said it is not necessary to get each value from an array to know the smallest value, $$anonymous$$athf.$$anonymous$$in will do that for you anyways, but, I'll tell you how to do it regardless. To get each value from an array you need to iterate over it with a for loop. Here's an example:
float[] arrayOfFloats = {1.0f,2.5f,3.0f,4.2f};
for (int i = 0; i < arrayOfFloats.Length; i++)
{
float currentValue = arrayOfFloats [i];
//Do whatever you want with currentValue, I'll just print it
Debug.Log(currentValue);
}
but it's not working ;-; i want the code to automatically get all elements values of the array then select the $$anonymous$$imum value, because i'm trying to make an time comparator, then this time comparator will select the car that gets to the destiny faster, when it gets all the car times values it shows in the screen the time of the winner(the winner needs to be the car that came to the destiny faster), i made another code that automatically gets the number of cars and make an array of it, then another ray gets the same Length of cars length to show the $$anonymous$$imum time of each car.
Your answer
![](https://koobas.hobune.stream/wayback/20220612144931im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mathf.Min, get which object is the minimum? 1 Answer
Assigning a different material to different objects 2 Answers
Array of Arrays 3 Answers
Array cannot read Greek characters 1 Answer
Randomize text position for 2D Quiz C# 0 Answers