playMaker

Author Topic: Whats the right way for a prefab to hide/show gameobjects in the scene?[SOLVED]  (Read 1765 times)

SamH

  • Playmaker Newbie
  • *
  • Posts: 41
I've been going in circles with this one.. I did find a work around but I'm curious if anyone has a simple way to do this that I'm missing..

Lets say I have a scene file which has a bunch of cube objects which are all identical prefabs. Each has a playmaker script which shows and hides a group of GUI text objects (which exist in the scene not the prefabs) when clicked. I'd like the all the text to be hidden by default, and only activate when a cube is clicked.   Prefabs can't directly reference scene objects, so I need the prefabs to reference a game object variable to interact with my GUI.

What I attempted:
1. I started with putting a FSM on a group which holds all the the gui text objects. It does a Find Object and declares itself as a global game object. I have the group de-active so they'd all be hidden at the start, waiting to be activated. This doesn't work because the deactivated game object doesn't run the FSM so it doesn't get found and stored in the global variable. (and if I use this method for all prefab interactions im gonna end up with a ton of globals=bad)

2. Next I tried making the group active by default. I made each prefab do an initial setup state where they each do a Find Game Object (using a tag on the gui text group) and store as a local variable. Then they instantly deactivate that group so its hidden. This doesn't seem to work (the variable comes up blank) I think because one of the prefabs is finding it and deactivating it a frame or so before other prefabs are finding it. Meaning the game object is not reliably found by all my cubes. Its also not great because I potentially have  a frame or two where the text is visible on screen before popping off.

3. I now realize that its probably a bad idea to be using the 'activate' feature, because it deactivates all the scripts potentially running on that object and it also has the above problems with not being able to be found. So I feel like its better to just activate/deacivate a sub property, like the Text(Script) or the canvas. So this element can be hidden by default but allow the top node to stay active. So I firstly find the gameobject in the prefab. Then I add a 'set property' action to my prefab FSM. BUT it doesn't look like I can use any global or local gameobject inside this action, which means prefabs are unable to use this action to interact with anything outside its own prefab.

So.. whats the best way to achieve this? It feels like there should be some 'hide' option which just makes the object invisible but keeps it (and any scripts on it) activate. The 'Set Visibility' action for some reason doesn't seem to work on my gui text.

My workarounds (none of which I like):
-Use a 'set text' to just remove the letters instead of hiding. But this wont work with say a GUI image
-Translate the GUI off screen and back on again. (This doesn't feel like a sensible way to handle it though..)

-Lastly what I've ended up doing is to have each prefab find the text group locally, wait for .1 of a second to ensure each instance has enough time to find it, then deactivate the group. It works but its not ideal.

Any help would be greatly appreciated!
ps playmaker is incredible.. loving it
« Last Edit: January 14, 2020, 03:22:08 AM by jeanfabre »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Whats the right way for a prefab to hide/show gameobjects in the scene?
« Reply #1 on: January 13, 2020, 02:37:52 AM »
Hi,

 I get this very often, it's a typical problem.

 I have three different ways to go about this:

1: I use global events,

each prefab has a reference ( could be the name, could be an fsm Int variable that you set when you instanciate them or a string that defines it.

each, UI item, also has this reference,

now prefab can fire a global event to all passing its reference in the event data, and each ui item receives it and check against this reference to see if it should act or not. It's very usefull when you want to have some kind of radio button for example, the current active UI item will receive the new order and detects that it's not the same reference and so it will turn itself off.

2: use a look up method, like findGameObject action, make use of tags to narrow down the search as finding objects by name is costly in big hierarchy, only do this once, and not in the game loop, cache the result of the finding as well.

3: the prefab also create the UI item it is reponsible for or linked to, that's also powerful, but it depends on your setup and the purpose of your feature. this way, you only need to know the parent of the UI items, or even send a global event passing the instance you just created so that any UI object can catch it and parent that instance to itself and then you don't event care where it is in the UI hierarchy,

Bye,

 Jean

SamH

  • Playmaker Newbie
  • *
  • Posts: 41
Re: Whats the right way for a prefab to hide/show gameobjects in the scene?
« Reply #2 on: January 13, 2020, 03:58:28 PM »
Thank you so much for your reply. I hadn't considered using global events to do this- I could even put a Set Property FSM on the UI element which turns on or off its canvas/text field (leaving its top node active). Then have prefabs send out a broadcast event to flip this state switch on or off. This would keep everything active, allow me to have the canvas switched 'off' by default and mean I don't even need to worry about finding gameobjects at all. That sounds pretty ideal!