- Home /
Question by
nerfman317 · May 12, 2014 at 05:02 AM ·
gameobjectmodelparentingassign
How to Change Parenting while in game? C#
I need to be able to change the parent of an object in a blender character model while in game. For now I have two models, one with it bound to the hand and one where it's bound to the back. I'm trying to use this script to deactivate and reactivate the two models mesh render.
if(Input.GetButton("f"))
//Sets the Boolean to true to activate animation.
animator.SetBool ("Sword", true);
//Sets the Chracter model with sword bound to hand to true.
SwordOut1 = GameObject.Find("Gamemodel Sword Combo")
SwordOut1.GetComponent<MeshRenderer>().enabled = true;
//Sets the Chracter model with the sword bound to the back to false.
SwordOut2 = GameObject.Find ("Gamemodel Combonation")
SwordOut2.GetComponent<MeshRenderer>().enabled = false;
}
else
{
//Sets the animation to not play anymore.
animator.SetBool("Sword", false);
//Sets the Chracter model with the sword bound to hand to false.
SwordIn1 = GameObject.Find("Gamemodel Sword Combo")
SwordIn1.GetComponent<MeshRenderer>().enabled = false;
//Sets the Chracter model with the sword bound to hand to true.
SwordIn2 = GameObject.Find ("Gamemodel Combonation")
SwordIn2.GetComponent<MeshRenderer>().enabled = true;
}
If you know any other way to fix this script or how to change the parenting of an models object than please reply.
Comment
Answer by snarf · May 12, 2014 at 06:53 AM
if(Input.GetButton("f"))
//Sets the Boolean to true to activate animation.
animator.SetBool ("Sword", true);
//Sets the Chracter model with sword bound to hand to true.
SwordOut1 = GameObject.Find("Gamemodel Sword Combo")
SwordOut1.GetComponent<MeshRenderer>().enabled = true;
SwordIn1.transform.parent = backPivotPoint // <---- Make this a public variable and link it to the back of your rig!
//Sets the Chracter model with the sword bound to the back to false.
SwordOut2 = GameObject.Find ("Gamemodel Combonation")
SwordOut2.GetComponent<MeshRenderer>().enabled = false;
}
else
{
//Sets the animation to not play anymore.
animator.SetBool("Sword", false);
//Sets the Chracter model with the sword bound to hand to false.
SwordIn1 = GameObject.Find("Gamemodel Sword Combo")
SwordIn1.GetComponent<MeshRenderer>().enabled = false;
//Sets the Chracter model with the sword bound to hand to true.
SwordIn2 = GameObject.Find ("Gamemodel Combonation")
SwordIn2.GetComponent<MeshRenderer>().enabled = true;
SwordIn2.transform.parent = handPivotPoint // <---- Make this a public variable and link it to the hand of your rig!
}