i was wandering how i would get an object from an array to use. im using bullitin array and i am looking for a way to sort the array by alphabet a-z,
at the moment im looking for a name of an object in the array, but i think it would be quicker if i knew exactly where it was, then i could use array instead of a loop function to find a name.
anyone know how i would do this.
If you are only randomly accessing the array, and if you don’t actually need to sort the list for any other purpose other than to speed up search time, then then my advice would be to use a hash table instead.
The benefit of a hash table is that you will get fast look ups (on average) without needing to take time to physically sort the list. There is a small amount of time spent constructing a hash key for each item that you add or retrieve from the table, but most key lookup’s are going to be O(1), and this is still considerably faster than sorting the list every time you want to look up an item. You also can compute the hash key for an item once and then store it somewhere for quicker retrieval, and that way object retrieval is practically as fast as array random access.
Here’s an overly simplified, but hopefully effective, demonstration of how to store some Unity GameObjects in a hash table and then look one up.
var table:Hashtable = new Hashtable();
function Start ()
var a:GameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
a.name = "Cue Ball";
var b:GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
b.name = "Billiard Chalk";
var c:GameObject = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
c.name = "Beer Glass";
// I want to find the Beer Glass object....
var key:String = "Beer Glass";
var obj:GameObject = table[key.GetHashCode()];
Debug.Log(obj.name); // Output: Beer Glass