Finding Game Objects, general questions with new format

I’ve started to get a better idea of how the Unity Editor works, to the point where I don’t have to go to the documentation as much to find answers to questions. I’m getting to the point now where I’m starting to think about best practices in regards to doing certain things with Unity. One situation that I keep running into is finding game objects that aren’t connected to each other. When I know there will be only 1 of them, I’ve been searching by name, but that uses a string reference and string references make me a bit uncomfortable. Similar problem with Tags.

Would it be acceptable to create an empty class/script on a game object that’s there purely for the purpose of searching? Is there a better way to do this sort of thing that doesn’t require a string reference?

Just off to bed, but I’m really glad you’ve made the new forum format work for you. Hope it’s solved in the morning :slight_smile:

Hi Zafaron,

If this class makes life easier for you or organises your project in a better way, go for it.

In my projects, I usually have two types of objects only in my Hierarchy:

  • unique instances
  • specific instances

I usually look for the former with FindObjectOfType() or GetComponent(). If I need the latter, I use [SerializeField] and assign them manually to my instance. In rare cases, I look for instances or game objects by passing on a string.

In my opinion, it depends on the case whether something is acceptable. Strings are not always bad, and FindObjectOfType() is not always the solution.

Did this help?

I have run into this problem most often when dealing with UI elements when using a singleton game object and moving between different game screens. The text that I want to update has often been on game objects that the singleton looses track of when moving between scenes.

The best way to do this just hasn’t quite clicked for me yet. I was able to make it work with a lot of Try/Catch blocks to get the correct objects from each scene, but the code made me cringe a little.

Thanks for your input as always.

Hi Zafron, link below for your reference.


See also;

Sorry about that, I’m used to the Q&A format over at Udemy. Will remember this for the future.

1 Like

No problem, we have proper built in functional wizardry here :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms