Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Unable to modify a material in real-time

Discussion in 'Unity 5 Pre-order Beta' started by Tabemasu-Games, Jan 28, 2015.

  1. Tabemasu-Games

    Tabemasu-Games

    Joined:
    Jul 25, 2014
    Posts:
    3
    When I try to modify certain values of a material in real time, it works the first time, then the material stops being updated, even though the inspector shows the correct value.

    A quick example :
    I want to fade out a mesh. I have a Standard Transparent Shader, on which I modify the alpha value every frame. First, I apply to it a copy of the material, so it will not change the color of every mesh using this material:

    Material material = new Material(meshRenderer.sharedMaterial);

    Then, each frame, I change the alpha channel:

    float alpha = Mathf.Lerp(maxAlpha, 0, time / duration);
    material.color = new Color(material.color.r, material.color.g, material.color.b, alpha);


    When I do that, the first frame is ok, the alpha has been reduced by a small amount (depending on time, duration and maxAlpha values, let's say alpha is 0.9).
    On subsequent frames, the alpha will always look the same, at 0.9, even though the inspector tells me the Albedo color has been correctly modified by the script (I still have to close/open the shader to see the modification though).

    Did I miss a step to say the material is "dirty" and needs to be updated? Or is it a Unity5 bug?
    I'm using Unity 5.0.0b18

    Thanks
     
  2. Tabemasu-Games

    Tabemasu-Games

    Joined:
    Jul 25, 2014
    Posts:
    3
    I found the problem: the Material constructor. If I don't use it, everything works perfectly.
    And Unity 5 seems to instantiate the material anyway when I change something like the color, so it's useless to do it manually.

    I sent a bug report via the reporter with a sample projet.