# How to find the minimum number in a float array C# Unity

i have a for loop that detects the position of every game object in a list and gets the difference between that object and an object anchored in the center and then stores all values in a float array. what should happen next is find the minimum distance in that array and make that object (the object with the smallest distance) move towards the anchored center. the problem is that no matter what i do, it always gets the maximum number (which is the furthest game object). here is what i have so far:

public RectTransform panal;
public GameObject[] characters;
public RectTransform center;
private float[] distance;
private float minDistance;
private bool isDragging = false;
private int distanceBetweenCharacters;
private int minCharNum;

private void Start()
{
int charLength = characters.Length;
center = GameObject.Find("CenterCompare").GetComponent<RectTransform>();
distanceBetweenCharacters = 200;
}

private void Update()
{
for (int i = 0; i < characters.Length; i++)
{
distance[i] = Mathf.Abs(characters[i].transform.position.y - center.transform.position.y);
}

float minDistance = Mathf.Min(distance);
Debug.Log(minDistance);

for (int a = 0; a < characters.Length; a++)
{
if (minDistance == distance[a])
{
minCharNum = a;
}
}

Debug.Log(minCharNum);

if (!isDragging)
{
LerpToChar(minCharNum * distanceBetweenCharacters);
}
}

void LerpToChar(int position)
{
float newY = Mathf.Lerp(panal.anchoredPosition.y, position, Time.deltaTime * 5f);
Vector2 newPosition = new Vector2(-386, newY);
panal.anchoredPosition = newPosition;
}

public void StartDrag()
{
isDragging = true;
}

public void EndDrag()
{
isDragging = false;
}


I assume that the problem is with the array because when i set the minCharNum to a specific integer, it works just fine. What am i doing wrong exactly? Any help is very much appreciated.

• What did you see when you attached the debugger? Is there a reason you're only using the y value to calculate distance?
– House
Sep 6, 2017 at 20:11
• @MichaelHouse Well, what I am trying to make here is a vertical scroll menu. I am only calculating the Y, simply, because I don't need the X. I will try to elaborate more... The point is to make a snapping scroll menu. I will be having around 10 characters (might increase in the future). So the program will calculate all the distances of all characters from a point that is anchored at the center of the screen. Then, I want to compare all the distances and get the minimum one so the character closest to the anchored point lerps towards that point. Sep 6, 2017 at 20:20

No reason for so many loops:

private void Update()
{
float minDistance = float.MaxValue;
for (int i = 0; i < characters.Length; i++)
{
distance[i] = (characters[i].transform.position - center.transform.position).sqrMagnitude;
if(distance[i] < minDistance)
{
minDistance = distance[i];
minCharNum = i;
}
}

if (!isDragging)
{
LerpToChar(minCharNum * distanceBetweenCharacters);
}
}


I would also suggest moving this outside of the Update method, if you don't actually need this every frame and if you have a large number of characters.

• Updated to use sqrMagnitude instead of Distance. Since you're not actually using the distance value, this will be a faster calculation.
– House
Sep 6, 2017 at 20:26
• Thanks for the answer. i tried to run the code but nothing happens. the minCharNum is always set to 0 (after i scroll up or down, it always goes back to the character at position 0). it's like it's not going through the if statement or its not meeting the condition. Any idea what to do? again, thanks for the help. Sep 6, 2017 at 20:52
• @Tarek-Dev 0 is a valid array index. That means that the first character is the closest. Sep 6, 2017 at 20:55
• @Draco18s that's true. But by scrolling up and down, it should no longer be the closest character. Nevertheless, it still goes back to position 0. Sep 6, 2017 at 20:59
• I suspect the issue is somewhere other than the code I've provided here. You're either not populating your characters list correctly, the center.transform is moving too or something else is happening.
– House
Sep 6, 2017 at 21:10