Converting String to a Game Object

Hi
I"m making a game and I need to convert the contents of a string to a game object. Now the docs say that I’m not supposed to use the find function every frame for performance reasons. However it offers two alternatives 1. find with tag which is easy to implement but I don’t see how its any more efficient. and 2. cache the result on startup which I can’t use because I need to reference different game objects and I need to do it when the program is running.

So my question is whats the best way to convert a string to a gameobject?

There are several ways of doing this, and the approach will largely depend on your specific needs.

One way I am using is this:

  1. Make all the game objects that need to be found a child of a master object.
  2. All child objects should have a unique name.
  3. The master object, on its Start(), uses foreach( Transform child in transform ) to find all its children, and add them to a Dictionary<string, GameObject> so that the key is the name of the object ( dictionary.Add(child.name, child.gameObject) )
  4. From this point on, any script that needs an object by string, will simply get the dictionary[‘SomeObjectName’]

Of course, this is the minimal and crud implementation, you should wrap the functionality in methods as per your individual design preferences.