animation.speed does not work when disabling/enabling renderer

Hi,

The scenario I have is: I’ve imported an animation and I am pausing/playing using animation.speed (animation.speed = 1 - Plays, animation = 0 - Pauses). This part works fine. I also wanted to have the animation disappear while it is paused. When I disable the renderer for the components in the animated model the model does disappear correctly but animation continues to play and it not at the pause point when I reset the animation.speed back to 1 and enable the renderer for the compnents. I am setting animation.speed to 0 before cycling through all the children of the object and disabling the renderer for each component.

I wanted to find out why disabling the renderer is effecting the speed value for the animation or is it an issue on how I am instantiating the prefab

Notes

  • I have the animation/model defined
    as a public GameObject and it is
    set in the inspector

  • I’ve checked that I am getting the correct instance name and the correct renderer states and also
    verified that the animation.speed value is getting set correctly on Pause to 0 and then back to 1

  • The animation object is instantiated
    when the user presses a button to
    play the animation (routine called
    from OnGUI()) and then the
    animation starts playing.

  • The Pause/Play work fine as long
    as I do not disable the renderer
    for the object components.

  • The Pause/Play code are separate routines called from inside the Update() function.

Has anyone run into this issue or have any suggestions. Thanks! (Please note, this is the first time I’ve posted in Unity Answers)

The basic code snippets are below.

    public GameObject Model;   //Prefab set in the Inspector
    ..
    ..
    
    Code to instantiate the object:
    
    AnimationInstance = (GameObject)Instantiate(Model,transform.position, transform.rotation)

  AnimationInstance.animation.Play();
    
    
    
    Code for Pausing the animation and disabling the renderer
    
                  AnimationInstance.animation["Take 001"].speed = 0.0f;
    			
    		
    		Renderer[] rendererComponentsOff = AnimationInstance.GetComponentsInChildren<Renderer>();
    		
            	// Disable rendering:
            	foreach (Renderer componentOff in rendererComponentsOff) {
    		   //Debug.Log("COMPONENT NAME for renderOFF = false: " + componentOff.name);
                	  componentOff.enabled = false;
            	}	

Code for starting the animation back up from the pause point 

        AnimationInstance.animation["Take 001"].speed = 1.0f;
				
        Renderer[] rendererComponentsOn = AnimationInstance.GetComponentsInChildren<Renderer>();
		
        				// Disable rendering:
        				foreach (Renderer componentOn in rendererComponentsOn) {
						//Debug.Log("COMPONENT NAME for renderON = true " + componentOn.name);
            				componentOn.enabled = true;
        				}

Your instantiate is fine. I would suggest recording the animations time when you pause the animation and set the time when you replay the animation after a pause. To get the time its:

AnimationInstance.animation["Take 001"].time