- Home /
Animator doesnt work correctly after disabling then enabling a gameobject
Hello, I've been having an issue with the unity Animator that im not sure how to get around: I have a FPS controller that when you hold right click, it raises your weapon via animation. These weapons are childs of a player transform parent (in a weapon holder). This worked fine, but then I added a weapon switch script that enables or disables a child weapon object based on if you have it equipped or not.
Once I started turning on and off the weapon gameobjects (both via the weapon switching script and just in editor while the game is running), the animator for that weapon stops working (doesn't play) for the raise and lower wep animations. Other animations in the same animator (i have a "quickshot" e.g.), seem ok oddly enough. I can see that the transitions for the animations are triggering correctly too, just there is no actual motion applied.
I've tried resetting all animator bools & triggers before disabling the game object, but that hasn't helped the issue either.
I've uploaded a youtube video with the animator state issue here : https://youtu.be/ej5QQYq1PJA
From what I can tell neither of these are the issue, but just in case here's the code for setting the raise weapon animator bool:
void LateUpdate () {
getFirstActive();
myWepAnim = firstActiveGameObject.GetComponent<Animator>();
if (Input.GetButton("Fire2") && quickShot.quickshotting == false)
{
myWepAnim.SetBool("raiseGun", true);
}
else if (Input.GetButtonDown("Fire2") == false)
{
myWepAnim.SetBool("raiseGun", false);
}
}
void getFirstActive()
{
for (int i = 0; i < gameObject.transform.childCount; i++)
{
if (gameObject.transform.GetChild(i).gameObject.activeSelf == true)
{
firstActiveGameObject = gameObject.transform.GetChild(i).gameObject;
}
}
}
And here's the code for the weapon switching (from this brackeys tutorial)
void selectWep()
{
int i = 0;
foreach (Transform weapon in rightWepHolder.transform)
{
if (i == selectedRightWep)
{
weapon.gameObject.SetActive(true);
}
else
{
weapon.gameObject.SetActive(false);
}
i++;
}
}
Answer by mxdestro · Aug 21, 2018 at 01:49 AM
Figured out the issue, my raise weapon animation didnt use a rotation parameter applied, but the lower weapon animation did set a rotation parameter. I don't know why it was working before but not after turning the gameobject on & off, but this did seem to fix the issue.