stuggling with activate/enable of various objects

Hoping someone can help. I’m working on a network game and when instantiating a new player I want to enable/disable the HUD whether networkView.IsMine is true or not.

I have an empty Game Object called HUD. In HUD I have several additional game objects such as “CockpitFlightControlPanel”
Inside these objects I have a combination of: Transform, GUITexture and a Script. Some don’t have GUITexture, just Transform and Script.

I want to fully disable them. If in the editor under Hierarchy I choose HUD, then in the inspector I click the activate check box at the top it asks if I want to deactivate all the children. When I perform the action this way it works fine. Now I’m trying to perform the same task in code. Below is a few examples of what I’ve tried. I’ve tried just using active, just using SetActiveRecursively, both together, flipflopping the order, etc… but nothing seems to work. I’ve also tried going at just the components(script) but haven’t had much success there either. This seems like it should be a simple, easy task, but it’s driving me crazy. Any suggestions what I might be doing wrong?

When I set a breakpoint I’m hitting this part of the code, so I know that’s working. The code I’m in is part of my player spawn code which is tied to the player prefab which gets instantiated when joining a new game. The network code loads the level then instantiates the player then the player spawn code runs and attempts to tweak the level appropriately. HUD is at the root level. Its not currently part of the player, although I’ve considered moving it to the player. I was having some alignment issues, but regardless, that shouldn’t be the root cause, right?

GameObject.Find(“HUD”).active = false;
GameObject.Find(“HUD”).SetActiveRecursively= false;

GameObject.Find(“CockpitFlightControlPanel”).active = false;
GameObject.Find(“CockpitFlightInformation”).active = false;
GameObject.Find(“EnergyGuages”).active = false;
GameObject.Find(“RadarControlPanel”).active = false;
GameObject.Find(“TargetingReticle”).active = false;

GameObject.Find(“CockpitFlightControlPanel”).SetActiveRecursively(false);
GameObject.Find(“CockpitFlightInformation”).SetActiveRecursively(false);
GameObject.Find(“EnergyGuages”).SetActiveRecursively(false);
GameObject.Find(“RadarControlPanel”).SetActiveRecursively(false);
GameObject.Find(“targetingReticle”).SetActiveRecursively(false);

I’ve even tried this:
(FlightControlPanel)(GameObject.Find(“HUD”).GetComponent(typeof(FlightControlPanel),true)).enabled = false;

Find will only return active GameObjects so that method would only work the first time you de-activate an object unless you cache a reference to the Find result. If Find is not null then SetActiveRecursively(false) should de-active the entire GameObject’s hierarchy.

So if I want to activate something that is deactive, how do I do it? Would I need to use tags? If so I’m really confused about tags. When I say add tag it shows me the level editor. I thought maybe they were interchangeable but I don’t see the predefined tags such as “camera” or “player” shown in the level view and when I add a name to an empty layer it only seems to provide a new layer to choose but nothing new in the tags area. Maybe this should be a separate post, but if I need to use tags to accomplish this then I could use some guidance here. If tags isn’t the answer to find a deactive object and active it then what is the right approach?

I also want to add I now have it working to deactive an active object. That wasn’t working but it turned out the line above it was failing and it was coming out of the function, but wasn’t giving me an error. I noticed this by stepping through the code and realizing it never stepped past the previous line. I then commented out that line and now the code works, but as mentioned I can only deactivate an active object. Now I want to reverse that and active a deactive object. I’ll continue to play, but any guidance would be appreciated.

Ahhhhh I got it working.

Ok for those who want to know:

I had a bunch of objects grouped under a single object called HUD.
I was trying to use Find(“HUD” but was told find can’t find a deactivated object so I couldn’t find HUD therefore I couldn’t active it or the things in it
What I realized is that I could activate HUD in the hierarchy, but deactive all the sub objects such as “CockpitFlightControlPanel”
Then in my code I can run GameObject.Find(“HUD”).SetActiveRecursively(true); and it works
since HUD was active, FIND works. However since every individual subobject was deactive, the HUD wasn’t displaying until I specifically want it to with the active.

Another option might be to make HUD a Prefab and just instantiate it when I want it, but it’s nice to have it working this way too, as now I better understand how find, findwithtags, findcomponents, etc… all works.

good programming :slight_smile: