- Home /
Using animator with RPC
Hello there,
I'm having an Issue with my animation, my models don't have Natives Animations set, so I'm using an Animator to anime them, but I got a problem with using it in Multiplayer.
For now I'm doing this to try animate:
[RPC]
void Run(string playerName)
{
GameObject Player0 = GameObject.Find(playerName);
Animator anim = Player0.GetComponent<Animator>();
anim.SetFloat("Speed", 1);
}
[RPC]
void Idle(string playerName)
{
GameObject Player0 = GameObject.Find(playerName);
Animator anim = Player0.GetComponent<Animator>();
anim.SetFloat("Speed", 0);
}
But in game I got Null Reference on anim.setFloat.
Does anyone have an idea on how to resolve that ?
Answer by manu3d · Jul 21, 2013 at 09:05 PM
By the look of it, the only thing that can be null is anim. Player0 can't be null, or the error would have been on the previous line. So, somehow unity thinks you do not have a component of type Animator attached to the object referenced by Player0. Add a Debug.Log(Player0) line immediately after the Player0 assignment line. Then select the Log output on the console. It should highlight the object referenced by Player0. Perhaps it's pointing to the wrong object?
Well it look like the animator is in a child (And i don't understand why because I put the animator on the parent) so I guess I should use getComponentInChildren
Perhaps you put the Animator on the parent but then instanced a prefab where the Animator is on a child ins$$anonymous$$d? It can be tricky sometimes to know where things really are when dealing with prefab and instances. getComponentInChildren() should be useful indeed. Or you can use GameObject.Find("parentName/childName").