Passing parameters between Mecanim controllers (potentially a dumb question)

I’ll start by saying that I am new to scripting/coding so this may be a really naive question.

I’m trying to set up an event whereby I have a player character under player input control and I want the character they are controlling to trigger a transition for another character that has thier own Mecanim animation flow controller.

So my scene has 2 Mecanim animation controllers; one for the playable character and one for another character, these two controllers have unique names; I’ve set up a trigger box in the scene and the idea is that when the player enters this box it triggers a bool in the other characters Mecanim animation controller that will allow a transition to take place for that character.

So far I’ve set up the trigger box and a script to switch a bool from false to true within the Player characters Mecanim anim controller; I can see this working… but how do I pass that bool over to be used by the other characters Mecanim animation controller?

I’ve tried creating the same name boolean in the second characters Mecanim anim controller but it doesnt switch to true as the Players one does.

I’m sure I’m missunderstanding something fundamental here about how it all works.

You’ll need to write a quick mirror script to do this, shouldn’t be too hard though.

using UnityEngine;

[RequireComponent(typeof (Animator))]
public class MecanimParameterMirror : MonoBehaviour
{
    [SerializeField] Animator _destinationAnimator;
    [SerializeField] string _parameterToMirror;


    int _parameterHash;
    Animator _sourceAnimator;

    void Awake(){
        _parameterHash = Animator.StringToHash(_parameterToMirror);
    }

    void Update(){
        // Note: I'm assuming you want to mirror a bool, but just change this to whatever you want to mirror
        _destinationAnimator.SetBool(_parameterHash, _sourceAnimator.GetBool(_parameterHash));
    }
}

I have not tested this, but it should work - and if nothing else it should be easy to debug. Basically just set the _destinationAnimator to your clone and the _parameterToMirror to the name of your parameter. Then, in the update loop it’ll sync the parameter.

Thanks Kyle that worked out great with a minor edit, one that I had to do some head scratching and guesswork to resolve at that.

I really am not a coder and this really is new to me so my approach to fixing it is likely very ugly and crude and I’m sure someone who knows what theyre doing, such as yourself, would have come up with an elegant solution.

Basically after implementing your script and figuring out what the heck it did and how (I’m still not 100% sure in truth as my knowledge of C# is very very limited to almost non existent, I’m a professional games animator by trade and coding has always been a mystery to me), I ended up figuring out that the NullExceptionError was something to do with the following line of code…

Animator _sourceAnimator;

So I sort of backward engineered part of the script, this part in fact…

[SerializeField] Animator _destinationAnimator;

Which I’d figured out gives me the input selector field for the destination Animator controller, and I simply copied it but for the source… I appreciate this shouldn’t be necessary as the source is the object the script is attached to and I can see how you were trying to define that but for whatever reason it just errored.

So the final script looks like :-

using UnityEngine;

[RequireComponent(typeof (Animator))]
public class MecanimParameterMirror : MonoBehaviour
{
[SerializeField] Animator _destinationAnimator;
[SerializeField] Animator _sourceAnimator;
[SerializeField] string _parameterToMirror;

int _parameterHash;
// Animator _sourceAnimator;

void Awake(){
_parameterHash = Animator.StringToHash(_parameterToMirror);
}

void Update(){
// Note: I’m assuming you want to mirror a bool, but just change this to whatever you want to mirror
_destinationAnimator.SetBool(_parameterHash, _sourceAnimator.GetBool(_parameterHash));
}
}

Once again, Big thanks Kyle

Whoops, sorry about that! I forgot to actually cache the source animator in Awake (this way you don’t have to set up the source in the inspector - it’ll just be whatever game object you’ve dropped the script on).

    using UnityEngine;
     
    [RequireComponent(typeof (Animator))]
    public class MecanimParameterMirror : MonoBehaviour
    {
        [SerializeField] Animator _destinationAnimator;
        [SerializeField] string _parameterToMirror;
     
     
        int _parameterHash;
        Animator _sourceAnimator;
     
        void Awake(){
            _sourceAnimator = GetComponent<Animator>();
            _parameterHash = Animator.StringToHash(_parameterToMirror);
        }
     
        void Update(){
            // Note: I'm assuming you want to mirror a bool, but just change this to whatever you want to mirror
            _destinationAnimator.SetBool(_parameterHash, _sourceAnimator.GetBool(_parameterHash));
        }
    }

Sorry about that :smile: