playMaker

Author Topic: Set visibility2 not working  (Read 1957 times)

wheretheidivides

  • Sr. Member
  • ****
  • Posts: 496
Set visibility2 not working
« on: June 01, 2018, 12:39:37 PM »
So I have a old project I just opened.  It could not find 'set visibility 2' so I had over 1000 errors.  I downloaded it from the ecosystem.  I manually traded the errors out with the downloaded visibility 2.

After running the game, it seems to not work.  It always turns the visibility off regardless of wether or not the enable is on or off.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Set visibility2 not working
« Reply #1 on: June 01, 2018, 06:53:22 PM »
Hi.
I got it from the ecosystem and it gave some red errors but unity solved them automatically on my end.
And the action seems to work on my end.
Using playmaker 1.9.0 and unity 2017.4.4f1

Here is the result from the script :

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2015. All rights reserved.
// Based on MaDDoX work http://hutonggames.com/playmakerforum/index.php?topic=159.msg591#msg591
/*--- __ECO__ __PLAYMAKER__ __ACTION__ ---*/

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Material)]
    [Tooltip("Sets or toggle the visibility on a game object. Can optionnaly reset on exit and recurse children visibility")]
public class SetVisibility2 : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;

        [Tooltip("Should the object visibility be toggled?\nHas priority over the 'visible' setting")]
        public FsmBool toggle;

        [Tooltip("Should the object be set to visible or invisible?")]
        public FsmBool visible;

        [Tooltip("Resets to the initial visibility once\nit leaves the state")]
        public bool resetOnExit;

[Tooltip("Affects children if set to true")]
        public bool recursive;

        private bool initialVisibility;

public override void Reset()
{
gameObject = null;
            toggle = false;
visible = false;
            resetOnExit = true;
            initialVisibility = false;
    recursive = true;
}

public override void OnEnter()
{
            if (gameObject.OwnerOption == OwnerDefaultOption.UseOwner)
                DoSetVisibility(Owner);
            else
                DoSetVisibility(gameObject.GameObject.Value);
           
            Finish();
}

        void DoSetVisibility(GameObject go)
{

if (go == null) return;

            var renderers = go.GetComponentsInChildren<Renderer>();

            // 'memorizes' initial visibility
            bool isRootRenderer = (go.GetComponent<Renderer>() != null);

            if (isRootRenderer)
                initialVisibility = go.GetComponent<Renderer>().isVisible;
            else
                initialVisibility = false;               

            // if 'toggle' is not set, simply sets visibility to new value
            if (toggle.Value == false) {
                if (isRootRenderer)
                    go.GetComponent<Renderer>().enabled = visible.Value;
               
                if (!recursive) return;
                foreach (var renderer in renderers) {
                    renderer.enabled = visible.Value;
                }
                return;
            }

            // otherwise, toggles the visibility
            if (isRootRenderer)
                go.GetComponent<Renderer>().enabled = !go.GetComponent<Renderer>().isVisible;

            if (!recursive) return;
            foreach (var renderer in renderers) {
                renderer.enabled = visible.Value;
            }
            return;
        }

        public override void OnExit()
        {
            if (resetOnExit)
                ResetVisibility();
        }

        void ResetVisibility()
        {
            // uses the FSM to get the target object and resets its visibility
            GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
            if (go != null && go.GetComponent<Renderer>() != null)
                go.GetComponent<Renderer>().enabled = initialVisibility;
        }

}
}

wheretheidivides

  • Sr. Member
  • ****
  • Posts: 496
Re: Set visibility2 not working
« Reply #2 on: June 01, 2018, 08:36:58 PM »
sorry,  I had reset on exit activated.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Set visibility2 not working
« Reply #3 on: June 02, 2018, 05:54:40 AM »
Hi.
Np, things like that even happens to the best :)

wheretheidivides

  • Sr. Member
  • ****
  • Posts: 496
Re: Set visibility2 not working
« Reply #4 on: June 02, 2018, 10:57:06 AM »
You know I did not have them set.  Maybe when I redownloaded the playmaker action it auto set them like that. 

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Set visibility2 not working
« Reply #5 on: June 02, 2018, 03:13:30 PM »
Hi,
Possible as i can see in the code that reset on exit is default set to true