playMaker

Author Topic: Activate / Deactivate Multiple Game Objects  (Read 8404 times)

joeyjr

  • 1.2 Beta
  • Playmaker Newbie
  • *
  • Posts: 12
    • View Profile
Activate / Deactivate Multiple Game Objects
« on: August 09, 2011, 09:41:11 AM »
I figured it might be use to have an action that can activate multiple game objects so I modified the existing action to work with multiple game objects.
I normally code in javaScript so this is my first attempt at C#, but I tested it and it appears to work fine.  It only took me all day to get it right! LOL, but this Action will save me and hopefully others a lot of frustration by now being able to consolidate all my Activated/Deactivated game objects into a single Action.

Also, it would be useful to have the Action name automatically change to "Deactivate" whenever the activate checkbox is unchecked so that when I'm reviewing all the actions on a particular state it would be easier and quicker to understand what has been coded. If anyone on this forum knows how to add that functionality that would be great!

Code: [Select]
// (c) copyright Hutong Games, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.GameObject)]
[Tooltip("Activates/deactivates multiple Game Objects.\nUse this to hide/show areas, or enable/disable many Behaviours at once.")]
public class ActivateMultipleGameObjects : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault[] gameObjects;

[Tooltip("Check to activate, uncheck to deactivate Game Object.")]
[RequiredField]
public FsmBool activate;
[Tooltip("Recursively activate/deactivate all children.")]
public FsmBool recursive;
[Tooltip("Reset the game objects when exiting this state. Useful if you want an object to be active only while this state is active.")]
public bool resetOnExit;


// store the game object that we activated on enter
// so we can de-activate it on exit.
GameObject activatedgameObjects;


public override void Reset()
{
gameObjects = new FsmOwnerDefault[1];
gameObjects[0] = null;
activate = true;
recursive = true;
resetOnExit = false;
}

public override void OnEnter()
{
DoActivateGameObject();

Finish();
}

public override void OnExit()
{
// the stored game object might be invalid now
if (gameObjects == null) return;

if (resetOnExit)
{
if (recursive.Value)
activatedgameObjects.SetActiveRecursively(!activate.Value);
else
activatedgameObjects.active = !activate.Value;
}

}

void DoActivateGameObject()
{
foreach (var gonum in gameObjects)
{
        GameObject go = Fsm.GetOwnerDefaultTarget(gonum);
if (go == null) return;

if (recursive.Value)
go.SetActiveRecursively(activate.Value);
else
go.active = activate.Value;

activatedgameObjects = go;
}

}
}

}
« Last Edit: August 09, 2011, 12:24:01 PM by joeyjr »

qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
    • View Profile
Re: Activate / Deactivate Multiple Game Objects
« Reply #1 on: August 09, 2011, 01:37:42 PM »
Cool thanks.. Was thinking of doing something like this..

Appreciate it!

Q

amaranth

  • Full Member
  • ***
  • Posts: 172
    • View Profile
Re: Activate / Deactivate Multiple Game Objects
« Reply #2 on: July 05, 2012, 12:41:26 PM »
This exactly what I needed. Thank you!

3d_Artist1987

  • Beta Group
  • Full Member
  • *
  • Posts: 157
    • View Profile
Re: Activate / Deactivate Multiple Game Objects
« Reply #3 on: July 05, 2012, 08:36:36 PM »
very useful!! :D

matrismatris

  • Playmaker Newbie
  • *
  • Posts: 1
    • View Profile
Re: Activate / Deactivate Multiple Game Objects
« Reply #4 on: June 18, 2014, 11:42:41 PM »
hi
this action not for unity 4
please upload for unity 4
thanks

Aaddiction

  • Junior Playmaker
  • **
  • Posts: 97
    • View Profile
Re: Activate / Deactivate Multiple Game Objects
« Reply #5 on: July 20, 2014, 05:51:12 AM »
Hello, this action is great, maybe should be in the official actions, but it shows some warnings on import. Please see attached. Is it possible to be updated?


PaulH

  • Playmaker Newbie
  • *
  • Posts: 48
    • View Profile
Re: Activate / Deactivate Multiple Game Objects
« Reply #6 on: August 16, 2014, 02:59:16 PM »
has this action been updated yet? its so handy and time saving. or been added to the standard action set?

thanks