Actual animation not resetting after deactivating then reactivating object
There seems to be a pretty major bug in Unity in that the Unity-made animation on my gun (the actual fire and reload animations were created using Unity's built-in animator) won't reset to default if I deactivate the object mid animation and then reactivate it again. The animation gets stuck in the exact place (position and rotation) of the fire or reload animation it was on when I deactivated it, even though the animator controller window indicates it's returned to the default idle animation and is playing that.
Does anyone know if there's a way to fix this?
This is a pretty major bug since my weapons can get stuck in a broken animation pose any time I switch through them. And, be it on re-enable or even if I press the fire or reload buttons again after re-enabling, it just then applies those animations over the top of the broken gun fire or reload animation pose rather than resetting the gun to its default pose and playing the animations correctly.
The issue can only be fixed if I reset the level entirely, which does then reset all the gun's animations and stuff again (its exact position and rotation and whatever is changed when you do an animation directly in Unity).
This is pretty much a Class A bug as you simply could not release a game like this--unless I'm missing something here (and I've tried a LOT of different ways to try and fix this issue).
So, again, is there any proper fix for this?
Answer by Zankomag · May 25, 2019 at 12:46 AM
The best way to fix this - disable MeshRenderer component of the gun instead of disabling entire GameObject. I faced to this issue with UI animation, so I ended up disabling Canvas component when I needed to disable object (later I've found out that it's also more efficient way, because Canvas won’t discard its vertex buffer and will keep all of its meshes and vertices, and when you re-enable it, it won’t trigger a rebuild, it will just start drawing them again). Thus for disabling Sprites you need to disable SpriteRenderer component. Also instead of making two scripts for disabling both 3D and 2D objects, you had better make one with Renderer component, because MeshRenderer and SprireRenderer inherit from Renderer.
Renderer renderer;
void Awake(){
renderer = GetComponent<Renderer>();
}
public void DisableObject(){
//When animation plays
//gameObject.SetActive(false); //DO NOT use this
renderer.enabled = false; //DO use this instead
}
Is there another solution other than @Zankomag 's disabling the renderer?
I've got a series of mechs pooling for a tower defense style game and need to disable the GameObject in order to reset all other aspects of the GO.
Currently, I end up with a growing majority of my mechs "crawling" along the path ins$$anonymous$$d of marching.