What Alternatives are there for GameObject.Find()

Hi all

I have just jumped back into using Unity after several months away. I’m looking for ways to store a reference to the UI panels I have made.

So far, i remember two ways, but I would like to know of other ways to store the reference so that I can use it in whichever class I feel like.

Option 1: GameObject.FindObjectWithTag(“xyzScreen”) - this of course can be used and I can create a local variable to hold the object that is returned. This works fine as long as it has a unique tag (I remmeber running into problems using this once I started making multiplayer games in past.)

Option 2: Drag n drop (inside Unity Inspector) a reference to each object into Public variable fields I made in the classes I want to use the screens with. nb. I hate doing this, I’d rather work 99.99999% in code if at all possible. And not to mention quite often the Inspector fields just become empty again and I have to drag references again. This to me feels like a workaround for not knowing the correct way to do this.

Option 3 (I know I said 2!) : Create the screens completely in code, and therefore instantiate and make active/inactive using variables I made whilst creating them.

So for my project today, which has led me to ask this question is:
I have a game with 4 possible ‘areas’ (all that means is the main viewport has 4 panels, Gym, Office, Home, Fightnight). I have a GameController.cs class which will be responsible for opening and closing each panel upon Button presses.

so my GameController.cs needs references to each of the UI panel objects.

I hope that makes sense and somebody can tell me the correct way for storing such references

THANKS

You can use simply GameObject.Find(“ObjectName”); if all of your 4 panel have differents name this will work.
Also if you script is in parent gameobject of your panels you can use Transform.Find(“ObjectName”).gameobject.

ok thanks mate. I was kinda aware of those, but for some unexplainable reason I thought that this was sort of the wrong way to do it. Glad to know that its probably just me being silly. I can of course easily name or tag each screen with a unique string.

I will do that for now, and in future when I doing multiplayer again I can use a different technique for ‘tagging’ other game objects. (I remember I used the Find(“”) methods etc and once I networked my game the screens would popup on everybodies screen because it was just finding all of them. The same was said for animations and other objects (my memories a bit hazey to say the least :P)

Thanks for your help though I think I can get on with it for now :smiley: