playMaker

Author Topic: Action to get all rigidbodies in children (not working)  (Read 898 times)

Rebooter

  • Playmaker Newbie
  • *
  • Posts: 21
Action to get all rigidbodies in children (not working)
« on: April 11, 2019, 01:04:01 AM »
I tried modifying an action to get all Rigidbodies in children from a gameobject, but it is just returning an empty array. I have an array variable of array type 'Object' with object type 'Rigidbody', and the gameobject is set to the root bone of a model.

Would anyone know why this does not work?


using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{

    [ActionCategory(ActionCategory.GameObject)]
    [Tooltip("Gets all rigidbodies on gameobject and its children recursively.")]
    public class GetAllRigidBodiesInChildren : FsmStateAction
    {

        [RequiredField]
        [Tooltip("The GameObject to get recursive children from.")]
        public FsmOwnerDefault gameObject;

        [Tooltip("if true, will list inactive gameobjects")]
        public FsmBool includeInactive;

        [RequiredField]
        [UIHint(UIHint.Variable)]
        [Tooltip("The Array Variable.")]
        public FsmArray array;

        private GameObject _go;
        private Rigidbody[] _result;

        public override void Reset()
        {
            gameObject = null;
            includeInactive = false;
            array = null;
        }

        public override void OnEnter()
        {
            Find();
            Finish();
        }

        void Find()
        {

            _go = Fsm.GetOwnerDefaultTarget(gameObject);

            if (_go == null)
            {
                return;
            }

            _result = _go.GetComponentsInChildren<Rigidbody>(includeInactive.Value);

            array.RawValue = _result;
        }
    }
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Action to get all rigidbodies in children (not working)
« Reply #1 on: April 11, 2019, 02:55:13 AM »
Hi,

 setting array is tricky, I think you need to call .SaveChanges() on your array

 if that doesn't work on it own, try to resize the array to the right value and set each indexes manually in a for loop.

 or try to set the.ObjectReferences property instead of .rawvalue

 let me know if you can't get it to work still...

 bye,

 Jean