Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity 5.3 - C# -- List<Vector2> How to extract biggest X-value?

Tags:

c#

unity3d

I'm developing a C# script on Unity 5.3. I have a list of Vector2 values and I need to extract the biggest X value in the list. I'm trying to do the following:

public List<Vector2> Series1Data;
... //I populate the List with some coordinates
MaXValue = Mathf.Max(Series1Data[0]);

However, I get the following errors:

error CS1502: The best overloaded method match for `UnityEngine.Mathf.Max(params float[])' has some invalid arguments
error CS1503: Argument `#1' cannot convert `UnityEngine.Vector2' expression to type `float[]'

Is there any other way of extracting the biggest X value in the list?

like image 248
XRR Avatar asked Mar 12 '23 13:03

XRR


2 Answers

You can do this via Linq:

MaxXValue = Series1Data.Max(v => v.x);

This assumes you Series1Data List object is not null or empty.

like image 25
Dean Goodman Avatar answered Mar 24 '23 21:03

Dean Goodman


You are trying to put a List on a function that can't have that type of variable as parameter.

Mathf.Max here you can see which type of parameters it can handle.

This code might do the work:

public List<Vector2> Series1Data;
... //I populate the List with some coordinates

MaXValue = Series1Data[0].x; //Get first value
for(int i = 1; i < Series1Data.Count; i++) { //Go throught all entries
  MaXValue = Mathf.Max(Series1Data[i].x, MaXValue); //Always get the maximum value
}
like image 114
josehzz Avatar answered Mar 24 '23 20:03

josehzz