Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Chimera3D · Aug 02, 2012 at 07:45 PM · arraygetcomponentint

Calculating the lowest number

I have a code where I'm getting values from scripts from objects in an array. I can get these values easily however, how can I compare them without having to do it manually since the array can get very long? What I need to do is compare each value to get the lowest value of them all, for example: I have 15, 20, 17, 81, 93, and 7. How can I get it to automatically find 7, I tried using Mathf.Min however I'll need a way to automatically enter in each value from each of the objects in the array with no persistent length?

EDIT: Okay what I've tried to do is join the contents of the array into a string separated by a comma and a space, however I can't compare this with Mathf.Min. How can I get this string to be usable in Mathf.Min?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jerdak · Aug 02, 2012 at 07:47 PM 0
Share

Unless each object tracks its lowest value, your problem is always going to be O(n) complex (worst-case) where n = total number of array elements from all arrays.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by fafase · Aug 03, 2012 at 09:48 AM

First off, this is just a variation in UnityScript of @ScroodgeM script. If your array is a declared as global (at the top) then you do not really need to pass it as it is accessible from anywhere in the script.

 function MinValue(): float
 {
     var end:int=values.Length;
     if (end <= 0)
         return 0.0;
     var minValue = values[0];
     for(var i:int = 1; i < end; i++) //iterate through the array
     {
         if (minValue>values[i])minValue=values[i]; // If the minimum is greater then assign the value.
     }
     return minValue; //Finally return that value 
 }

Now if the array is not global (in case)

 function MinValue(values : float[]): float
 {
     var end:int=values.Length;
     if (end <= 0)
         return 0.0;
     var minValue = values[0];
     for(var i:int = 1; i < end; i++)
     {
         if (minValue>values[i])minValue=values[i];
     }
     return minValue;
 }

call it like

case 1:

 var min : float = MinValue();

or case 2:

 var min : float = MinValue(floatsArray);

EDIT:Updated the answer with the suggestions from @Eric5h5.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 03, 2012 at 11:31 AM 1
Share

This is even better than using $$anonymous$$athf.$$anonymous$$in. I really like this function and i use it quite often, but in this case it's just slows it down. $$anonymous$$ath.$$anonymous$$in also does the if comparison, but regardless of the condition, $$anonymous$$Value is always reassigned each iteration.

Note: the behaviour of this function is a bit strange when you pass an empty array: It returns infinity.

I would do it like this, but it of course depends on what behaviour you expect:

 // Unityscript
 function $$anonymous$$inValue(values : float[]) : float
 {
     if (values.Length <= 0)
         return 0.0;
     var $$anonymous$$Value = values[0];
     for(var i = 1; i < values.Length; i++)
     {
         if ($$anonymous$$Value > values[i])
             $$anonymous$$Value = values[i];
     }
     return $$anonymous$$Value;
 }
avatar image fafase · Aug 03, 2012 at 12:07 PM 0
Share

I guess you won't $$anonymous$$d if I update my answer with those relevant information. I just think of later consultations, I like when answer are updated so that I don't have to go all over the thread to play lego with the little pieces of updates.

avatar image Eric5h5 · Aug 04, 2012 at 01:32 AM 0
Share

For best speed you should use

 var end = values.Length;
 for (var i = 1; i < end; i++)

Also, technically .Length should be compared to an int (and it's not possible for it to be less than 0), and ideally should check for null, so:

 if (values.Length <= 0.0)

I would write as

 if (values == null || values.Length < 1)
avatar image Bunny83 · Aug 04, 2012 at 02:01 AM 0
Share

Yes and yes ;) the 0.0 was a typo of course, but i like <= 0 more than < 1

avatar image
1

Answer by ScroodgeM · Aug 02, 2012 at 07:56 PM

solution 1

write a simple method getting array returning min value, it will take O(n) time but will be easiest way to implement

pseudocode

float MinValue(float[] values)
{
    float minValue = Mathf.Infinity;
    for(int i = 0; i < values.Length; i++)
    {
        minValue = Mathf.Min(minValue, values[i]);
    }
    return minValue;
}

to calc min value use:

float min = MinValue(floatsArray);

solution 2

write your simple array class with storing a min value while adding elements to it.
Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chimera3D · Aug 02, 2012 at 08:06 PM 0
Share

Ok but how can I do the first solution as in I need pseudo code.

avatar image ScroodgeM · Aug 02, 2012 at 08:11 PM 0
Share

read answer

avatar image Chimera3D · Aug 02, 2012 at 08:33 PM 0
Share

How can I get this to work in JS?

var $$anonymous$$inValue : float(fScore.cellScore : float[]){

avatar image ScroodgeM · Aug 02, 2012 at 08:35 PM 0
Share

i think 'function', not 'var' in method declaration

avatar image Chimera3D · Aug 02, 2012 at 08:52 PM 0
Share

How am I supposed to call this function, saying $$anonymous$$inValue(); isn't working?

Show more comments
avatar image
1

Answer by Meltdown · Aug 03, 2012 at 09:45 AM

 int[] iArr = { 5, 2, 3, 1, 4 };
 Array.Sort(iArr);
 Debug.Log(iArr[0]);

After sorting iArr[0] returns the lowest number.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chimera3D · Aug 03, 2012 at 08:42 PM 0
Share

Thanks, that worked nicely.

avatar image Bunny83 · Aug 04, 2012 at 01:08 AM 1
Share

Well, this is one of the shortest solutions, but also the most inefficient one ;) You sort the whole array just to get the lowest one. Afaik Array.Sort uses Quicksort (at least in Unity). So the complexity is on average O(n log n) or in the worst case O(n²)

avatar image Chimera3D · Aug 22, 2012 at 06:06 AM 0
Share

For some reason I can't implement the $$anonymous$$value function. :/

avatar image Eric5h5 · Aug 22, 2012 at 01:26 PM 1
Share

Sure you can.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# - Problem with trigger that won't activate 1 Answer

C# Getting a certain AnimationState using strings from all objects with specific tag 2 Answers

JS: Array index not taking array.length as valid int? 2 Answers

Any way to 'GetComponent' as a base class? 2 Answers

Manipulate arrays in Inspector with Editor Script? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges