Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Material.SetFloat not updating in play-mode.

Discussion in 'Scripting' started by oen432, Jul 3, 2019.

  1. oen432

    oen432

    Joined:
    Apr 24, 2015
    Posts:
    40
    I'm trying to update
    _Fill
    property of my shader using
    Material.SetFloat
    property. It works just fine, however it's not updating in play-mode but only after stopping.
    Using
    setMaterialDirty()
    on
    Image.material
    didn't work.

    Code (CSharp):
    1.  public class OrbFillUpdater : MonoBehaviour
    2. {
    3.      public IntegerReference Variable;
    4.      public IntegerReference Max;
    5.      private Material material;
    6.      private void Start()
    7.      {
    8.          material = GetComponent<Image>().material;
    9.      }
    10.      private void Update()
    11.      {
    12.          float value = Mathf.Clamp01(Mathf.InverseLerp(0, Max.Value, Variable.Value));
    13.          material.SetFloat("_Fill", value);
    14.      }
    15. }
    And here is
    _Fill
    property.
    Code (CSharp):
    1.  _Fill("Fill Amount", Range(0, 1)) = 1
    2.  
    3. float _Fill;
    4.  
    5. fixed4 frag(v2f IN) : SV_Target {
    6.      // ... Code
    7.      if (IN.texcoord.y >= _Fill)
    8.      {
    9.          if (IN.texcoord.y + (noise.r - 0.7) * _NoiseStrength >= _Fill)
    10.          {
    11.              color.a = 0;
    12.              layer1.a = 0;
    13.              layer2.a = 0;
    14.              layer3.a = 0;
    15.          }
    16.      }
    17. }
     
  2. oen432

    oen432

    Joined:
    Apr 24, 2015
    Posts:
    40
    Still looking for a way to fix this.
    Maybe this is a Unity bug?
     
  3. oen432

    oen432

    Joined:
    Apr 24, 2015
    Posts:
    40
    Bump