First of all, I have a character with 2 armatures (Upper/Lower Body), where the Upper Body is the child of the Lower Body's root bone (Hip). This is intended, and I really don't want to touch the hierarchy, as it would break a lot of other things.
The thing is, that the animator of the Lower Body works like a charm and reacts to SetTrigger as expected (although in a different script, but on the same GameObject), while if I try to do the same for the Upper Body, it just throws me the following error and does nothing:
Parameter 'Attack' does not exist. UnityEngine.Animator:SetTrigger (string)
The Trigger does exist with the proper name (no typo), set up on the right animator with the right transitions. If I trigger it from the animator on spawn, it does play it, so the Trigger does exist.
Here is part of the code:
[SerializeField] GameObject goal;
[SerializeField] Transform stance;
[SerializeField] ParticleSystem projectileParticles;
[SerializeField] float targetRange = 10f;
[SerializeField] float attackRange = 5f;
[SerializeField] Transform enemy;
public GameObject Target;
public float maxDistance;
public NavMeshAgent agent;
public Animator animUpper;
public Animator animLower;
public void Start()
{
animUpper = GetComponentInChildren<Animator>();
animLower = GetComponentInChildren<Animator>();
}
void Update()
{
FindClosestEnemy();
AttackEnemy();
}
public void FindClosestEnemy()
{
List<GameObject> nearestEnemies = new List<GameObject>();
GameObject lastAddedEnemy = null;
GameObject enemyTemp = null;
float maxDistance = Mathf.Infinity;
Vector3 position = transform.position;
enemyTemp = FindNearestWithTag("RightBrute");
if (enemyTemp != null && enemyTemp != lastAddedEnemy)
{
nearestEnemies.Add(enemyTemp);
lastAddedEnemy = enemyTemp;
}
enemyTemp = FindNearestWithTag("RightRanged");
if (enemyTemp != null && enemyTemp != lastAddedEnemy)
{
nearestEnemies.Add(enemyTemp);
lastAddedEnemy = enemyTemp;
}
enemyTemp = FindNearestWithTag("RightArmor");
if (enemyTemp != null && enemyTemp != lastAddedEnemy)
{
nearestEnemies.Add(enemyTemp);
lastAddedEnemy = enemyTemp;
}
foreach (var enemy in nearestEnemies)
{
Vector3 diff = enemy.transform.position - position;
float currentDistance = diff.sqrMagnitude;
if (currentDistance < maxDistance)
{
enemyTemp = enemy;
maxDistance = currentDistance;
}
}
Target = enemyTemp;
}
public GameObject FindNearestWithTag(string tagName)
{
GameObject[] enemies;
enemies = GameObject.FindGameObjectsWithTag(tagName);
GameObject closest = null;
float maxDistance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (var enemy in enemies)
{
Vector3 diff = enemy.transform.position - position;
float currentDistance = diff.sqrMagnitude;
if (currentDistance < maxDistance)
{
closest = enemy;
maxDistance = currentDistance;
}
}
return closest;
}
public void AttackEnemy()
{
if (Target == null)
{
stance.LookAt(goal.transform);
Attack(false);
Move();
}
else
{
float maxDistance = Vector3.Distance(transform.position, Target.transform.position);
stance.LookAt(Target.transform);
if (maxDistance < targetRange)
{
Approach();
if (maxDistance < attackRange)
{
Attack(true);
animUpper.SetTrigger("Attack");
//GetComponentInChildren<Animator>().SetTrigger("Attack");
}
}
else
{
stance.LookAt(goal.transform);
Attack(false);
Move();
}
}
}
public void Attack(bool isActive)
{
var emissionModule = projectileParticles.emission;
emissionModule.enabled = isActive;
}
public void Move()
{
NavMeshAgent agent = GetComponent<NavMeshAgent>();
agent.SetDestination(goal.transform.position);
animLower.SetFloat("Speed", agent.velocity.magnitude);
}
public void Approach()
{
NavMeshAgent agent = GetComponent<NavMeshAgent>();
agent.SetDestination(Target.transform.position);
animLower.SetFloat("Speed", agent.velocity.magnitude);
}
As you can see I've tried using GetComponentInChildren().SetTrigger("Attack"); but it resulted in the same error. I've been at it for days now and I'm completely out of ideas. I beg for some help, because the way I see it from other questions, it is a bug with Unity itself, in which case I would really like to get a workaround and a cold compress.
Reproduction steps with example character:
Yellow: Hierarchy; Orange: Script in question; Red: Properly assigned Animators;
Orange: Upper Body Animator; Red: Trigger set up properly; Trigger is set up on two transitions for testing purposes. Changing it doesn't fix anything. I originally wanted it to come from Any State only.
Animated character FBX files: https://we.tl/t-m8Yo1yMttS
Complete, simplified example project: https://we.tl/t-7P3FFwK8tv
To test the issue, just hit Play, go back into Scene and drag the Cube over the character. It gives the exact same results.