After animating a custom material/shader parameter, how do I get an accurate value from script?

For example, I create a new shader with a Color parameter, then create a new material that uses that shader.

I assign that material to a new 3D object, and use an Animator (on another object) to animate that object’s material’s Color values. Hitting Play, I can see the object changing color correctly.

However, I have a component on the object where I just want to query the current animated color from the material.
e.g.

void Update {

Debug.Log(GetComponent().material.GetColor(“_Colour”));

Debug.Log(renderer.material.color);

Debug.Log(GetComponent().sharedMaterial.GetColor(“_Colour”));

Debug.Log(renderer.sharedMaterial.color);

}

All of these only output the value that the Color attribute was BEFORE playing, not while the animation is live. How can I get the live animated value?

Just found an answer! I noticed a message in the inspector saying that a MaterialPropertyBlock is used to control the values. I guess the animator does that during playmode.

Reading from the property block solves this issue:

MaterialPropertyBlock properties = new MaterialPropertyBlock();
renderer.GetPropertyBlock(properties);
Vector4 color = properties.GetVector("_Color");
Debug.Log("color: " + color);

Try use the LateUpdate event, it works the same way but gets called after the animations.

Check this out Unity - Manual: Order of execution for event functions

Been wracking my brain trying to figure this out and scouring forums for this. Was trying to get the animation > animation.clip > animation.clip.property(curves, component, etc) so Thank You!