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!
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
}
some side-notes on performance. you can ignore these if your array is small.
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.
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.
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.
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
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.
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
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
![](https://koobas.hobune.stream/wayback/20220612101212im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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
Array of scripts on component 1 Answer