Forum rules - please read before posting.

Link Variable To Animator bug

edited January 2023 in Technical Q&A

Currently, the "Link Variable To Animator" script from the most recent AC version (not the old one from the wiki) doesn't correctly link global variables to parameters due to the following lines:

            if (variables == null)
            {
                ACDebug.LogWarning ("No Variables component found for Link Variable To Animator on " + gameObject, this);
                return;
            }

            if (_animator == null)
            {
                ACDebug.LogWarning ("No Animator component found for Link Variable To Animator on " + gameObject, this);
                return;
            }

            if (string.IsNullOrEmpty (sharedVariableName))
            {
                ACDebug.LogWarning ("No shared variable name set for Link Variable To Animator on " + gameObject, this);
                return;
            }

variables is always null when you are not using component variables, so the return stops the rest of the script from running.

Comments

  • This change allows "set variable" actions that change the global variable to affect the animator parameter, but overall the script still behaves very erratically (it won't synchronise the parameter on start/load, so starting the game or leaving the scene and coming back will leave the parameter unsynchronised).

    And if you check "script sets initial value?" (not something I want to use, but I thought I'd test it anyway), for some reason the script will only change the global variable to match the animator parameter half the times I run the scene, and I'm not sure why this is inconsistent.

  • variables is always null when you are not using component variables

    Thanks for the bug report - it's a silly issue, and a fix is already scheduled to be included in the next update.

    To fix in the meantime, replace line 54 with:

    if (variableLocation == LinkableVariableLocation.Component && variables == null)
    

    overall the script still behaves very erratically (it won't synchronise the parameter on start/load, so starting the game or leaving the scene and coming back will leave the parameter unsynchronised).

    Is it the variable value that's correct, or the parameter?

    the script will only change the global variable to match the animator parameter half the times I run the scene, and I'm not sure why this is inconsistent.

    As in, exiting-entering Play mode multiple times only causes the two to become linked every other time?

    The two should become synced only when accessed - i.e. checking/setting the variable with Actions. It should be noted that if you only want the link to go one-way (variable -> parameter), you can remove both the compoment and the variable's Link to property, and replace it with a much simpler script:

    using UnityEngine;
    using AC;
    
    public class VarToAnimParam : MonoBehaviour
    {
    
        public string intName;
        public string boolName;
    
        public void LateUpdate ()
        {
            if (!string.IsNullOrEmpty (intName)) GetComponent<Animator> ().SetInteger (intName, GlobalVariables.GetVariable (intName).IntegerValue);
            if (!string.IsNullOrEmpty (boolName)) GetComponent<Animator> ().SetInteger (intName, GlobalVariables.GetVariable (intName).BooleanValue);
        }
    
    }
    
  • Thanks! That's exactly what I was looking for, a one-way link from variable to animator.

    As in, exiting-entering Play mode multiple times only causes the two to become linked every other time?
    

    Yes, this. I'm not sure why this was happening. If the parameter was 2 and the variable was 3, the variable would only become 2 some of the times I entered Play mode. The rest of the times, it'd remain 3.

  • If the parameter was 2 and the variable was 3, the variable would only become 2 some of the times I entered Play mode. The rest of the times, it'd remain 3.

    Would it be possible for you to PM me a repro package/project that I can test this behaviour with?

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.