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. 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.
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.
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.
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).