- Home /
How can I change a controller's model at runtime without breaking mecanim?
I've got a gender select screen and based on this data, in the next scene, the character controller has a model/avatar assigned to it.
During testing I dragged and dropped a model/avatar onto the controller (male or female) and everything worked fine. However, when I assign the model/avatar 'on the fly' it doesn't work. The avatar just floats around.
I'm sure I'm missing something small but I don't know what it could be. Any help would be appreciated.
CODE:
public GameObject MaleModel;
public Avatar MaleAvatar;
public GameObject FemaleModel;
public Avatar FemaleAvatar;
public GameObject Player;
GameObject model;
// Use this for initialization
void Start () {
int boy = PlayerPrefs.GetInt ("Gender");
if (boy != 0) {
model = Instantiate(MaleModel);
Player.GetComponent<Animator>().avatar = MaleAvatar;
} else {
model = Instantiate(FemaleModel);
Player.GetComponent<Animator>().avatar = FemaleAvatar;
}
model.transform.SetParent (Player.transform);
model.transform.localPosition = new Vector3 (0, 0, 0);
}
Answer by MamaCatDev · Jul 19, 2015 at 07:00 PM
After a long bit of trial and error, I discovered that the solution to this was to Rebind the Animator after setting the parent and local position.
model.transform.SetParent (Player.transform);
model.transform.localPosition = new Vector3 (0, 0, 0);
Player.GetComponent<Animator>().Rebind();
This was confusing the crap outta me. Thanks for this!
Your answer

Follow this Question
Related Questions
Get list of parameters of an AnimatorController 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Single animation controller on multiple objects (Not working) 0 Answers
NavMeshAgent With Mecanim 3 Answers
Javascript to control parameters of animator controller 3 Answers