find object in array using name instead of index

Hi Guys,

I’m trying to find a game object in an array by using the name of the object rather than an index number. The name I want to find changes so I’m using a variable. Is there an easy way to search an array for an object name? I’m getting an invalid cast exception when I try to find (name in Objects) because I’m trying to reference a game object with a string.

Thanks in advance for the help!

if you want to find them by name you must iterate through the whole array and check the name if it is right.

if you don’t want to use index access at all, use hashtable / Dictionary<string,xxx> instead of array to have a key based collection

Thanks. I’ve been thinking of using a hashtable. I guess that’s the best way. I’m not terribly familiar with them, but have been reading up.

Since I’m here, I have one other array question. I know there’s an easy way to convert from a Javascript array to a built in array – array.ToBuiltin(), but is there an easy way to go from a built in array to a Javascript array? I didn’t see anything on it in the scripting manual or in the forums.

Thanks again.

You can pass a builtin array as a parameter to the JS array’s constructor to fill it with the contents of the builtin:-

var newarr = new Array (builtinArray);

Perfect.Thanks!

The method Dreamora mentioned for going through the array and checking names is not particularly cumbersome if you want to stick with standard arrays.

for(int i = 0; i < array.Length; i++)
{
  if(array[i].name == desiredName)
  {
    //Do Stuff
  }
}

I don’t find that cumbersome at all and I used to do it all time with Lua’s tables. Its actually alot easier to use if you add objects to the array as the game goes on.

EDIT: Ignore that I misread the post… :sweat_smile:

Thanks to all of you. This is a real help. What I’m ultimately trying to do is make a weighted or biased array. I’m making a word game and want to weight certain letters more than others in a random choice.

From what I’ve read, the easiest way to do that is simply to duplicate the letters you want to appear more often. So if the alphabet has 26 letters and I put in three letter A’s, the odds go up roughly three times that it will get selected if I use Random.Range (0,array.length). To do this, I need to search for and then duplicate letters (in this case, the letters are 3d game objects). Thus my need to find them by name from a string. In other words, if user inputs letter “A”, I need to find gameobject A in the array and then add copies. It’s a lot tougher problem than I originally thought. If you guys have a more elegant way of doing this, I’d appreciate any thoughts.

Thanks again for your help! :smile:

There is a random picking/shuffling library in this thread. The Sample function takes an array of floats that represent the relative frequency of each option (so if element 0 contains 13.134 that means that the relative frequency of the index 0 is 13.134, etc). You just need an array with 26 float elements and set each element to the relative frequency of the corresponding letter (so element 0 will be A, 1 will be B, etc).