Search Unity

Changing material properties at runtime

Discussion in 'Editor & General Support' started by HonoraryBob, Jun 22, 2019.

  1. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,214
    Is there any way to make a new (unique) material at runtime, modify it, and then assign it to a renderer's material (in that order) rather than the usual method of modifying a material already attached to a renderer? I need to do it in that sequence given the way my code is set up; but It doesn't work if I change the properties on the new material itself but only if I change it on the material assigned to a renderer, such as the following code:

    Code (CSharp):
    1. rend.materials[j].SetFloat("_StencilRef",Frequency);
    But the following doesn't work:

    Code (CSharp):
    1. NewMaterial.SetFloat("_StencilRef",Frequency);
    I remember reading somewhere that this is expected behavior; but is there an alternate way to do it?
     
  2. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,214
    There must be a way to just modify the properties in a new material variable before applying the material to the renderer, but I can't get it to work. Anyone know?