playMaker

Author Topic: Trying to activate then destroy an object please help  (Read 5069 times)

Cratz

  • Playmaker Newbie
  • *
  • Posts: 21
Trying to activate then destroy an object please help
« on: July 24, 2016, 09:08:35 PM »
Please Help,

This is what I want to accomplish..
I have about 40 objects and each one has its own button. They are all deactivated then When a button for one of the objects is pressed then I want all the other objects to be destroyed/ deactivated, and the one pressed to be activated. So If anyone has a suggestion of what the best way to do this is, please let me know.
Thank you

Zeldag

  • Full Member
  • ***
  • Posts: 198
Re: Trying to activate then destroy an object please help
« Reply #1 on: July 25, 2016, 01:19:27 PM »
Hi and welcome to the forum !

So you will have 40 buttons, one for each game object?

And are all objects and buttons present in the scene (so they are not prefabs that you create during runtime?)?

If so a simple way to do it:

Make a button (whichever way you do it-if you're not sure describe what button you need and I will try to explain how to make it). Adds an FSM to the button.
In the first state of the button FSM add whichever actions you use for your button (eg: 'ugui button on click' or something like that), and send an event when the button is clicked to state 2.
In state2; use 'activate object' and instead of 'use owner' choose 'specify game object' then drag the game object from the hierarchy into the 'game object' space. https://hutonggames.fogbugz.com/default.asp?W52

Then duplicate the button 40 times and drag a different object into each one.

This way each button will activate a different game object, while the rest remain deactivated.

This is just a simple way of turning on one game object. If you need any further explanation, let me know (like how to destroy/deactivate the rest of the buttons/game objects).

Good luck with it, hope this helps.

Cratz

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Trying to activate then destroy an object please help
« Reply #2 on: July 25, 2016, 08:08:30 PM »
Thanks for the reply.

So I have buttons from unity UI not gui from playmaker. heres what I'm trying to do.
I have 40 objects lets say I have object 1 active, then I click on object 24. I want object 1 to be deactivated if I do that, I want that done for all, that no matter what button I press it will activate it and deactivate any one that I pressed. But I already have the buttons set up from unity. I dont want to use the gui from playmaker.

Can you explain it in a lot of detail. I'm very new to playmaker. Thank you

Zeldag

  • Full Member
  • ***
  • Posts: 198
Re: Trying to activate then destroy an object please help
« Reply #3 on: July 27, 2016, 03:31:13 PM »
Quote
Can you explain it in a lot of detail. I'm very new to playmaker. Thank you

Hi,

I can sure try. But I will also try to keep it simple, like no arrays or anything, just global event sending may be as complicated as it gets.

So lets get cracking lol:

For the Game Object:

Create an FSM on the object.

Create two global events in the FSM (create an event and click the global tick box). Call them : "Activate" and "DeActivate".

Now keep the start state blank, dont put any action in it and just create state 2.

Right click on state 2 and choose 'add global transition', then add the 'Activate' global event that you just created.

In state 2, add this action: 'Send Event', choose 'event target' as 'broadcast all' and be sure to tick the 'exclude self' tick box. Choose 'DeActivate' as the event to send.

Next create state 3. Again right click and add the DeActivate global event so it transitions into state 3.

In state 3: add this action: 'Activate Game Object' and leave 'use owner' for 'gameObejct'. Then add a finish event and link it to the start state (state 1- which should have no actions in it or events transitioning out of it).


For the Button:
Create a button (GameObject/Create>UI>Button).

Then go here: https://hutonggames.fogbugz.com/?W1192 and download the "Package for uGUI Proxy With Full set of Actions" (it is the second link under the first image).

Import the downloaded package into unity and this will give you the actions you need to communicate with Unity UI.

Then set up an FSM on the button:

State 1: Add the action "U Gui Button On Click Event" (it comes in the action pack). And for 'Send event:' create something like "button pressed" and add it to the state and then select it. This event should go to state 2.

State 2: Use the action 'Activate Game Object', select specify game object (instead of use owner) and drag the first object into the 'game object' space. Then untick the 'recursive' box.

Use the send event action and set the 'event target' to "game object". Then again, specify game object and drag the first game object into the space. For 'event' chose to send the 'Activate' event, that you created on the game object earlier.

NOW! If you just add a finish event and link back to the first state, you will have made an on button. (So when you press the button the object is activated, every other game object will be disabled, but if you press it again, the button does not turn the object it turned on, back off) Read on if you want to make a toggle (on/off) button.

Now copy and paste state 1, to make state 3.
Then link a finish event from state 2 to state 3.

On state 3, right click, chose 'add event', go down to 'custom events' and add the 'DeActivate' event, that you created earlier on the game object, so that it transitions out of 'state 3' into 'state 1'.

Then copy and paste state 2, to make state 4.

Then connect the 'button pressed' event from state 3 into state 4

In State 4: Delete the 'Send Event' Action. Then on the 'activate game object' action, untick 'activate' (this will disable the game object). Add a finished event and link back to the first state.

:D and voila! If you managed to get through that, youre done!

Now copy and paste, either the button itself, or just the fsm to all the buttons. Then just drag and drop the correct item into all the item boxes. So that each button corresponds to a specific item. Do that 40 times and youre all set.

Here is a video of it in action and a general walk through around the setup:  (Never mind my blue Ethans running around, I just put this together in a middle of a concept scene I was working on- as a means of taking a break lol- also I was talking to the video, but apparently had sound recording turned off lol, so it is just a silent video :( )

Good luck with it and I hope this helps!

(PS: this tutorial also should help understand unity ui in playmaker: )
« Last Edit: July 27, 2016, 03:43:52 PM by Zeldag »

Cratz

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Trying to activate then destroy an object please help
« Reply #4 on: July 27, 2016, 05:14:05 PM »
Hi!

Thank you for all that info! I managed to get the objects to activate when the ui button is pressed. The only thing is, its not disabling the other objects when I press a button. Basically when any of the buttons is pressed only 1 object should be showing.

I've attached some screen shots, let me know what I missed. Thanks

Zeldag

  • Full Member
  • ***
  • Posts: 198
Re: Trying to activate then destroy an object please help
« Reply #5 on: July 27, 2016, 05:35:18 PM »
Hi,

Having a quick look, in your button FSM, state 2; you should make 'activate game object' then 'send event' (otherwise the event gets sent to a deactivated object), just like in the last screenshot of my last post.

Secondly in state 3 of the game object, you should untick the 'activate' box in the 'activate game object action'. (This state should deactivate the object but if the box is ticked you're just activating it again).

I think that was all I could spot for now.

Try it and hopefully it will work.

Good luck with it, hope this helps.

(PS, I would also untick all the recursive boxes, on every 'activate game object' action. It basically activates each child individually, may be annoying if your objects ever have children. You can find out more about actions here:  https://hutonggames.fogbugz.com/default.asp?W52 )
« Last Edit: July 27, 2016, 05:41:18 PM by Zeldag »

Cratz

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Trying to activate then destroy an object please help
« Reply #6 on: July 28, 2016, 06:34:23 PM »
Worked perfectly! Thank you! Question tho. The events Activate and Deactivate are they defined? How does the system define them? Could I have typed anything? Or is Activate and Deactivate some type of key word?

Zeldag

  • Full Member
  • ***
  • Posts: 198
Re: Trying to activate then destroy an object please help
« Reply #7 on: July 28, 2016, 07:04:32 PM »
Hi!

It is awesone to hear that it is all working for you.

You are correct, the activate/deactivate event names can be called anything you want. As long as you mark it as global and then send whatever you called it. I called them that as this is mostly what the event did.

Good luck with whatever you tackle next and don't forget to go through some playmaker tutorials, I learned loads from them and still do.

Cratz

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Trying to activate then destroy an object please help
« Reply #8 on: July 28, 2016, 09:23:04 PM »
Hey ! I found a problem! So When I click on the button everything works fine the button makes the object appear but when I click on another button then click back on the original button nothing appears. The children are not getting activated it seems.


coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Trying to activate then destroy an object please help
« Reply #9 on: July 29, 2016, 08:26:32 AM »
if its the children not getting activated then use the recursive checkbox

https://hutonggames.fogbugz.com/default.asp?W52

Cratz

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Trying to activate then destroy an object please help
« Reply #10 on: July 29, 2016, 06:16:27 PM »
Hey thanks! Yeah I read back what you said and realized you mentioned it already. Thanks again. One thing tho, seems that when I run the apk on my phone it shows "state 3" on all the buttons. Anyway to get rid of that?

Zeldag

  • Full Member
  • ***
  • Posts: 198
Re: Trying to activate then destroy an object please help
« Reply #11 on: July 30, 2016, 06:11:27 PM »
Hi,

Although i did mention the recursive box previously, i didn't actually get a chance to reply in regards to your last query about the children, that was Coxy :)  . Glad you've got it working though. (An important thing to remember with 'recursive" is that if you keep it ticked when you disable an object, then you must keep it ticked when you re-enable that object. This is because recursive disables (unticks the enabled box in the inspector for) the parent and then each child individually, whereas without recursive it only enables or disables the parent object that is targeted by the action. So if you disable recursively you disable each child individually, but if you then enable non-recursivley- then the parent gets reenabled while the children stay disabled.

Otherwise if you disable just the parent, non-recursively then the children disappear automatically anyway as the children of a disabled object automatically get disabled but actually stay enables in their own inspector- so then when you reenabled the parent object non-recursivley they automatically becone active again. Sorry if this is just reads like a complete nonsense ramble, but there is some logic here somewhere, I promise :) .)

The state 3 showing is not actually related to anything we did in particular, it is just a general playmaker feature.

It shows state labels so that you can debug and generally see that your states are flowing correctly during game play (is very useful when you build for mobile, but cannot get unity remote to work, like in my case :( ).

You can turn it off in playmaker preferences:

Open playmaker editor.

In bottom right corner click on 'preferences'

Then if I remember correctly you need to untick the option 'show playmaker state labels in game view'.

Then I'm pretty sure it will all be fine.

(You can also turn off state labels for each FSM individually, in the inspector panel.

Good luck!

PS. You can rename the states and FSMs as well as the events, to anything that makes sense in your game (so for the buttons you could do something like renaming 'state 1' to 'wait for touch' and the FSM to 'button control').
« Last Edit: July 30, 2016, 06:30:45 PM by Zeldag »

Cratz

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Trying to activate then destroy an object please help
« Reply #12 on: August 03, 2016, 10:47:32 AM »
Hey ! Thank you! Its all working fine, yeah sorry I realized I responded to a person I thought was also you haha. Thanks again!