Search Unity

Bug: Animated material color always returns default value.

Discussion in 'Animation' started by Ziya, Apr 10, 2017.

  1. Ziya

    Ziya

    Joined:
    Aug 30, 2013
    Posts:
    65
    I use Mecanim animation system to animate material color of Renderer and always get default value of color in script despite the color changes at runtime. The same doesn't apply to other properties.
    If I set Loop Time and Write Defaults to false, I will get a value of color set back to default after animation completes.
    How can I get current color? The only way I know to get color is to use "curve.Evaluate(normalizedTime);".

    The code I use to get material color:
    Code (CSharp):
    1. Debug.Log(GetComponent<Renderer>().material.color.ToString());
    Reproduce:
    1. Create a cube.
    2. Create material.
    3. Set the material to the cube.
    4. Go to Window/Animation.
    5. Click "Create" button.
    6. Set curves to animate from white color to red color by duration of 1 second.
    7. Don't change other properties of the animation. "Loop Time" is true. "Write Defaults" is true.
    8. Write script to return a value of material color at runtime, when you press a button.
    9. Play the game.
    10. You see the color of the cube changes from white to red and the animation starts again every time.
    11. Press a button many times and you will always get the same default value - white color.

    Mentions of it in other threads:
    https://forum.unity3d.com/threads/4-3-material-color-animation-broken.212400/
    https://forum.unity3d.com/threads/write-defaults-confusion-bug.392058/
     
    Last edited: Apr 11, 2017
  2. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    Can you share the code you're using to read the material color?
     
  3. Ziya

    Ziya

    Joined:
    Aug 30, 2013
    Posts:
    65
    I edited my question.
     
  4. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    Can you try with
    Debug.Log(GetComponent<Renderer>().sharedMaterial.color.ToString());
    ?
    I get the feeling (from the documentation) that using renderer instantiates a new material instance, which fits with the symptoms.
     
  5. Ziya

    Ziya

    Joined:
    Aug 30, 2013
    Posts:
    65
    No, it is same.
     
  6. thornebrandt

    thornebrandt

    Joined:
    Mar 22, 2015
    Posts:
    15
    I'm still experiencing this exact bug 5 years later. Was there ever a fix or a workaround?