- Home /
How do I find the greatest value in a list?
I have a list "heights" containing items as floats. I want to set a variable "highestValue" that updates every frame and returns the value of the greatest value in the list.
Floats are constantly being added and removed from the list, but I want to search through the list for the highest value every frame. Does anyone know the best way I could do that.
I'm using unity Javascript if it matters.
Answer by Bunny83 · Feb 12, 2013 at 07:53 PM
Since you have floats you can use Mathf.Max and pass the list converted to an array.
var myList : List.< float >;
// [...]
var maxValue = Mathf.Max(myList.ToArray());
The name 'List' does not denote a valid type ('not found'). Did you mean 'UnityEngine.Light'?
Help? :D
@Friduric: This question is about the generic List type which is defined in the System.Collections.Generic namespace. So you have to import that namespace at the top of your script:
// Unityscript
import System.Collections.Generic;
// C#
using System.Collections.Generic;
Like this?:
#pragma strict
@script ExecuteInEdit$$anonymous$$ode()
import System.Collections.Generic;
private var vignetting : Vignetting;
// Settings. public var maxIntensity : float = 5.0; public var $$anonymous$$Intensity : float = 0.0;
I just get error: Assets/Scripts/Graphics/Image Effects/$$anonymous$$yScript.js(1,1): BCE0044: unexpected char: 0xFEFF.
Answer by FL · Feb 12, 2013 at 07:22 PM
Use Array.Sort() and arrayName[arrayName.length-1].
It's not really the "best way" since he don't want to sort the whole list. He just needs the greatest value.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
missingGameObject problem 3 Answers
How to use Javascript Array with classes? 1 Answer
Spawning a list of objects with XML? 0 Answers
Insert a string to a list inside a list 3 Answers