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

Having trouble modifying a property of a Material using Material.SetFloat()

Discussion in 'Shaders' started by mycatsarefat369, Apr 4, 2022.

  1. mycatsarefat369

    mycatsarefat369

    Joined:
    Aug 9, 2021
    Posts:
    10
    I am using URP and have made a shader graph and attached it to a Material, of which I have set as some GameObject's Material. Changing the values of a float variable of the Material seems to work in Editor prior to and during runtime, but through code the modification of the values does not work for the material. Also, when printing the value of this float, it shows me the value which I had set it to in code, and it doesn't print the value which I set it to in Editor. Does anybody know how I can alternate the situation in the sense that modifying the float's value will take effect for the Material? Thank you very much for your time.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,255
    Make sure you’re setting the correct property. The name of the property from code is not the one that you initially set on the material or in Shader Graph. You need to use the Reference name.
     
  3. mycatsarefat369

    mycatsarefat369

    Joined:
    Aug 9, 2021
    Posts:
    10
    upload_2022-4-5_10-28-31.png
    upload_2022-4-5_10-28-41.png
    I'm currently using the reference of the property. Perhaps looking at these settings and my code might help you detect something which is out of place.
     
  4. mycatsarefat369

    mycatsarefat369

    Joined:
    Aug 9, 2021
    Posts:
    10
    Wait it works in one part of my code so I'm going to take a look at it

    I was debugging it once, before fixing my reference which I only did after your note of advice, and decided to set the value to 0f if the player wasn't jumping. The player wasn't jumping, since I tried debugging using Input.GetKey("k"); Well, in any case your advice saved me, and now I have a working float value for my material. Thank you very much.
     
    Last edited: Apr 5, 2022