Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by danielrgrn · Dec 20, 2017 at 05:51 PM · arraysmathfelementsminimum

How to get all array float values then do Mathf.Min?

this is the code :

     void Update()
     {
         vencedor = Mathf.Min(/*gets all array float values then select the minimum value*/);
     }

does someone know how to do it?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by MacDx · Dec 20, 2017 at 06:05 PM

Mathf.Min can already accept an array of floats and return the smallest one of the array. I don't know where your array comes from so I'll just give you a simple example.

 void Update()
 {
     float[] arrayOfFloats = {1.0f,2.5f,3.0f,4.2f};
     vencedor = Mathf.Min( arrayOfFloats);
 }

In this case Mathf.Min will return 1.0f and thus 1.0f will be assigned to vencedor, since that's the smallest value in the example array I wrote.

Hope this helps!

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 danielrgrn · Dec 20, 2017 at 06:14 PM 0
Share

i'm sorry i forgot to say not this i want, because the array gets all objects in scene, and i don't want to specify the number of elements in array :

 using UnityEngine;
 using System.Collections;
 
 public class GameStatistics : $$anonymous$$onoBehaviour {
 
     public float[] tempos;
     public GameObject[] carros;
     public float vencedor;
 
     void Start()
     {
         carros = GameObject.FindGameObjectsWithTag("Carro");
         tempos = new float[carros.Length];
     }
 
     void Update()
     {
         vencedor = $$anonymous$$athf.$$anonymous$$in(/*gets value of elements in array the select the $$anonymous$$imun value*/);
     }
 }
 

i want it to get each value of each element in array then select the $$anonymous$$imum, and i don't know how to automatically get each value

avatar image MacDx danielrgrn · Dec 20, 2017 at 06:26 PM 0
Share

i'm sorry i forgot to say not this i want, because the array gets all objects in scene, and i don't want to specify the number of elements in array

I did not "specify" the length of the array in my example, it could be any array of float created however you want, with whatever arbitrary length, getting values from some objects in the scene or whatever. The point is that $$anonymous$$athf.$$anonymous$$in does not care how you construct the array.

i want it to get each value of each element in array then select the $$anonymous$$imum, and i don't know how to automatically get each value

Like I said it is not necessary to get each value from an array to know the smallest value, $$anonymous$$athf.$$anonymous$$in will do that for you anyways, but, I'll tell you how to do it regardless. To get each value from an array you need to iterate over it with a for loop. Here's an example:

 float[] arrayOfFloats = {1.0f,2.5f,3.0f,4.2f};
 for (int i = 0; i < arrayOfFloats.Length; i++)
 {
     float currentValue = arrayOfFloats [i];
     //Do whatever you want with currentValue, I'll just print it
     Debug.Log(currentValue);
 }

avatar image danielrgrn MacDx · Dec 20, 2017 at 06:34 PM 0
Share

but it's not working ;-; i want the code to automatically get all elements values of the array then select the $$anonymous$$imum value, because i'm trying to make an time comparator, then this time comparator will select the car that gets to the destiny faster, when it gets all the car times values it shows in the screen the time of the winner(the winner needs to be the car that came to the destiny faster), i made another code that automatically gets the number of cars and make an array of it, then another ray gets the same Length of cars length to show the $$anonymous$$imum time of each car.

Show more comments
Show more comments

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

120 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mathf.Min, get which object is the minimum? 1 Answer

Assigning a different material to different objects 2 Answers

Array of Arrays 3 Answers

Array cannot read Greek characters 1 Answer

Randomize text position for 2D Quiz C# 0 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