Forum rules - please read before posting.

Custom Action loses references until action list is refreshed?

edited November 2022 in Engine development

Hello, I'm having an issue where, whenever I restart unity, one of my custom action list loses its references.

The action list is for triggering multiple animation parameters at the same time, therefore I need to reference animators from the scene. Normally it works great, the animators trigger and is persistent if I change from one scene to another thanks to the custom ID.

The issue is, when I close and open unity again and I play the action list that has this custom action, a debug error I made, that states that there are no animators, appears in my console and nothing happens. If I reopen the action list where the custom action is playing from, everything is as it should be an it displays the references correctly in the editor. After being reopened, if I play the scene again, the action doesn’t give me any error and works as it should.



This means I have to reopen every single action list that uses this custom action everytime I restart Unity.

            // Declare variables here
            public int animatorLength;
            public int runetimeAnimatorLenght;
            public int animatorLenghtParameterID = -1;

            public List<Animator> animators = new List<Animator>();
            public List<Animator> runtimeAnimators = new List<Animator>();

            public List<int> animatorsConstantIDs = new List<int>(10) { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            public List<int> animatorsParameterIDs = new List<int>(10) { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };

            public string parameterName;
            public enum ParameterType { trigger, boolean, intNum, floatNum }
            public ParameterType parameterType;
            public float floatValue;
            public int intValue;
            public bool boolValue;


public override void AssignValues(List<ActionParameter> parameters)
        {
            runetimeAnimatorLenght = AssignInteger(parameters, animatorLenghtParameterID, animatorLength);
            for (int i = 0; i < animators.Count; i++)
            {
                if (runtimeAnimators.Count > i)
                {
                    runtimeAnimators[i] = AssignFile<Animator>(parameters, animatorsConstantIDs[i], animatorsParameterIDs[i], animators[i]);
                }
                else
                {
                    Animator runtimeAnim = AssignFile<Animator>(parameters, animatorsConstantIDs[i], animatorsParameterIDs[i], animators[i]);
                    runtimeAnimators.Add(runtimeAnim);
                }
            }
        }

public override float Run()
        {
            if (!isRunning)
            {
                isRunning = true;
                //Debug.Log("RUNNING MULTI ANIMATOR");
                if (string.IsNullOrEmpty(parameterName))
                {
                    Debug.LogError("Parameter name is empty");
                }

                for (int i = 0; i < runtimeAnimators.Count; i++)
                {
                    if (runtimeAnimators[i] == null) Debug.LogWarning("Animator is null, remeber to assign the animators");
                    switch (parameterType)
                    {
                        case ParameterType.trigger:
                            if (runtimeAnimators[i])
                            {
                                runtimeAnimators[i].SetTrigger(parameterName);
                            }
                            break;
                        case ParameterType.boolean:
                            if (runtimeAnimators[i]) { runtimeAnimators[i].SetBool(parameterName, boolValue); }
                            break;
                        case ParameterType.intNum:
                            if (runtimeAnimators[i]) { runtimeAnimators[i].SetInteger(parameterName, intValue); }
                            break;
                        case ParameterType.floatNum:
                            if (runtimeAnimators[i]) { runtimeAnimators[i].SetFloat(parameterName, floatValue); }
                            break;
                        default:
                            break;
                    }
                }
            }

            if (!isRunning)
            {
                isRunning = true;
                return defaultPauseTime;
            }
            else
            {
                isRunning = false;
                return 0f;
            }
        }


public override void ShowGUI(List<ActionParameter> parameters)
        {
            int OldNumber = animatorLength;
            animatorLenghtParameterID = Action.ChooseParameterGUI("Transition time (s):", parameters, animatorLenghtParameterID, AC.ParameterType.Integer);
            if (animatorLenghtParameterID < 0)
            {
                animatorLength = EditorGUILayout.IntSlider("Aniamtors", animatorLength, 1, 10);
            }
            if (animatorLength < 1)
            {
                animatorLength = 1;
            }
            int Dif = OldNumber - animatorLength;
            if (Dif > 0)
            {
                animators.RemoveRange(animatorLength, Dif);
            }
            else if (Dif < 0)
            {
                for (int Count = 0; Count < -Dif; Count++)
                {
                    animators.Add(null);
                }
            }
            for (int i = 0; i < animators.Count; i++)
            {

                if (animatorsConstantIDs.Count <= i)
                {
                    animatorsConstantIDs.Add(0);
                    Debug.Log("Count: " + animatorsConstantIDs.Count + " i: " + i);
                }
                animators[i] = (Animator)EditorGUILayout.ObjectField("Animator " + i, animators[i], typeof(Animator), true);
                animatorsConstantIDs[i] = FieldToID<Animator>(animators[i], animatorsConstantIDs[i]);
                animators[i] = IDToField<Animator>(animators[i], animatorsConstantIDs[i], true);
            }
            parameterName = EditorGUILayout.TextField("Parameter Name:", parameterName);
            if (string.IsNullOrEmpty(parameterName))
            {
                EditorGUILayout.HelpBox("Parameter name must not be empty", MessageType.Warning);
            }
            parameterType = (ParameterType)EditorGUILayout.EnumPopup("Parameter Type:", parameterType);

            switch (parameterType)
            {
                case ParameterType.trigger:
                    break;
                case ParameterType.boolean:
                    boolValue = EditorGUILayout.Toggle("Bool Value", boolValue);
                    break;
                case ParameterType.intNum:
                    intValue = EditorGUILayout.IntField("Int Value", intValue);
                    break;
                case ParameterType.floatNum:
                    floatValue = EditorGUILayout.FloatField("float Value", floatValue);
                    break;
                default:
                    break;
            }
        }

        public override string SetLabel()
        {
            // (Optional) Return a string used to describe the specific action's job.

            return string.Empty;
        }

Comments

  • Your runtimeAnimators variable need not be public - this will cause it to become serialized, whereas it should only be set at the point the Action is run.

    Privatize runtimeAnimator, delete runetimeAnimatorLenght, and replace your AssignValues function with:

    public override void AssignValues(List<ActionParameter> parameters)
    {
        for (int i = 0; i < animators.Count; i++)
        {
            Animator runtimeAnim = AssignFile<Animator>(parameters, animatorsConstantIDs[i], animatorsParameterIDs[i], animators[i]);
            runtimeAnimators.Add(runtimeAnim);
        }
    }
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.