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 AndrewRyan · Nov 22, 2016 at 09:00 PM · arraystaticmethodcomponentsgeneric

Generic arrays?

Hi,

Simply for convenience, I am trying to create a static method that returns the closest transform in a passed array. Is it possible to read components off of a generic array?

 public static Transform FindClosestInArray <T>(T[] array, Vector2 origin)
     {
         Transform nearest = null;
         float minDist = Mathf.Infinity;
 
         //loop through all to find nearest
         for(int i=0; i<array.Length;i++)
         {
             float dist = Vector2.Distance(array[i].transform.position, origin);
             if(dist < minDist)
             {
                 nearest = array[i].transform;
                 minDist = dist;
             }
         }
 
         return nearest;
     }

edit: Thanks everyone for the assistance!

Comment
Add comment · Show 2
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 Sergio7888 · Nov 22, 2016 at 10:47 PM 1
Share

I recommend you to use a where T : Component on your function declaration.


public static Transform FindClosestInArray<T>(T[] array, Vector2 origin) where T : Component
     {
//Your code here
     }
avatar image elenzil · Nov 22, 2016 at 11:47 PM 1
Share

some side-notes on performance. you can ignore these if your array is small.

  1. accessing the transform of a component is somewhat expensive. if you're able to cache all those transforms outside of this call and then just pass in an array of Transforms, you'll be ahead.

  2. similar to 1) - if you can't cache the transforms outside this routine, at least cache the transform from line 9 locally, so that you're not looking it up again on line 12.

  3. you may get a $$anonymous$$or speed-boost by replacing Distance(a, b) with (a-b).sqr$$anonymous$$agnitude(). the square of the magnitude is perfectly valid for testing whether one vector is shorter than another, and avoids an internal call to squareRoot(). but again, if your array is small, i'd stick with what you have for clarity.

1 Reply

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

Answer by R1PFake · Nov 22, 2016 at 09:04 PM

Your array Elements T inherit from MonoBehaviour? If yes then you could write : public static Transform FindClosestInArray (T[] array, Vector2 origin) where T : MonoBehaviour that means you can pass any class which inherits from MonoBehaviour and then you can access the .transform since its a property of MonoBehaviour

Comment
Add comment · Show 3 · 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 · Nov 22, 2016 at 09:14 PM 1
Share

The type "Component" would make more sense ^^. $$anonymous$$onoBehaviour is derived from Component and it's actually the Component class that introduces the "transform" property.

avatar image R1PFake Bunny83 · Nov 22, 2016 at 09:28 PM 0
Share

That's right, i assumed that his objects are $$anonymous$$onoBehaviours anyways and was too lazy to check which base class introduces the transform property :D

avatar image AndrewRyan · Nov 23, 2016 at 02:15 AM 0
Share

Thanks, R1P. That appears to work for everything save Gameobject arrays. As far as I know, I have to, reluctantly, convert a Gameobject array to w/e component array I need for this process. Please yell at me if this is abysmally inefficient.

     public static Transform FindClosestTransformByTag(string tag, Vector2 origin)
     {
         GameObject[] gos = GameObject.FindGameObjectsWithTag(tag);
         Transform[] ts = new Transform[gos.Length];
         for(int i=0;i<gos.Length;i++)
         {
             ts[i] = gos[i].transform;
         }
         return FindClosestInArray(ts, origin);
     }

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

65 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

Related Questions

Storing functions to use later 0 Answers

method to display random question in C# 3 Answers

Static method performance vs non static 0 Answers

Generic use of Components through typeof(Component) | Convert from Unity's generic Component into a desired one 1 Answer

Array of scripts on component 1 Answer


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