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