So the ability to form an array out of conditions and cycle through it is very useful. Not sure how to do this, I looked on teh internets and found the Array.some(var x : function with condition);
Unfortunately, ‘some’ is not a member of Array, so this doesn’t work in Unity. Anyone know of an equivalent or better way to do this?
Here is an example of how Array.some() works.
var numbersarray=new Array(-3, -34, 1, 32, -100);
function isZeroAbove(element, index, array) {
return (element > 0);
}
if (numbersarray.some(isZeroAbove)) //evaluates to true
print("One or more elements inside array is above 0 in value!");
name this script some.js
drag it onto a game object
call this function in your other scripts like this:
if (some.isAbove(ArrayName,targetValue){doThis;}else{doThat;}
This first part will test the code. Change the values in the array to see the different results. (take this out of the script if you plan to use it)
var numbersarray=new Array(-3, -34, 1, 32, -100);
function Update (){
if (isAbove (numbersarray, 0)) {
Debug.Log("One or more elements inside array is above 0 in value!");
}else {
Debug.Log("Didn't find a high enough value in this array.");
}
}
Here is the code you need:
/*I'm listening for a target value so that the same function can be used to see if any array entry is higher than any value*/
static function isAbove (dataset : Array, target : int) : boolean {
var count : int = 1;/*counter to see where we are in the process (counter MUST start at one because we are comparing NUMBER OF ENTRIES NOT which index)*/
var number : int;/*we will read the values out of the array one at a time and evaluate them to see if there is one above zero*/
for (var entry: int in dataset){/*for each entry*/
number=entry;/*store the value of the entry in the number variable*/
if (number < target count == dataset.length){return false;}/*we can only return a false if we are at the end of the array and the last entry is less than target*/
if (number > target) {return true;}/*we can return true any time any value is above target*/
if (count < dataset.length) {count++;}/*we didnt find what we were looking for and we aren't at the last entry so increment the counter*/
}
}
Lots of other stuff possible here but this will do what you need.