Why is my Avatar getting unassigned from the Animator at runtime?
I am building a multi-player networked game. When I spawn my character at runtime the avatar gets unassigned from the Animator.
I can re-apply the reference manually and the character will animate, Unfortunately, Animator.Avatar is not fully supported and Unity advises to use it at your own risk.
Why is this happening? I am having trouble finding any sort of fix.
Answer by ADAMN721 · Apr 28, 2017 at 08:09 PM
I have had a similar problem before with my networked game and ended up discovering it was because the item I was referencing wasn't attached to my player. For networked games, any item you are assigning in the inspector has to be a part of that prefab or it will lose it's reference when the player is instantiated. For example, if you have a static object in the scene such as a cube. You can't have a reference that you drag and drop in the inspector for your player on that item.
In your scenario that may not apply but you can always assign it through script at runtime. Try implementing an Awake() method and assigning it in there.
Thanks for the quick reply,
I made the same assumption, but assigning at runtime does not seem to be working. Here is my code, your help is appreciated:
public Avatar Robot$$anonymous$$yleAvatar;
private Avatar avatar;
void Awake()
{
anim = GetComponent<Animator> ();
avatar = anim.GetComponent<Avatar> ();
avatar = Robot$$anonymous$$yleAvatar;
}
So for assigning at runtime you want to make sure that you are referencing the correct place in the hierarchy. Currently, you are saying the animator component is on the same level your script is on, is this correct? If your animator is on a lower or higher level of the prefab, this will fail to assign.
For an example, say the script is on the top level of your player and the animator is attached to a child gamobject of that player. You would need to use GetComponentInChildren() in that case.
Side note: I noticed you have a public and a private variable. You could just make one private variable and use [SerializeField] attribute which will force unity to serialize a field for it in the inspector.
The animator is on the same level as the script. There is no example in the API of how to assign an avatar via script.
$$anonymous$$y public Avatar is the robots avatar while my private Avatar is the Avatar component of the Animator.
Is this method incorrect?
So it is a humanoid rig, and it does animate correctly. The problem only occurs when I spawn the network character. As soon as it is spawned the avatar is unassigned.
During runtime, I can manually drag the avatar onto the animator and the character begins to animate.
Although the rig is humanoid, the avatar does not auto assign its self.
HI @ADA$$anonymous$$N721 I tried adding the character to the scene without spawning it. Since there are network components on the character it becomes inactive at runtime.
As soon as a check the box to enable the character, the avatar gets unassigned. When I remove the network components the problem is gone.
Any idea of what might be happening here?