Problem setting material texture propertys on script

Hello, I am trying to set the main texture tiling of a Plane primitive object.

  • I have the Plane, the plane has exactly 1 material that shows visually, as we would expect.
  • to set the properties on Inspector works fine.

The following script doesn’t work (it gives me no visual change):

// GameObject grounds = ...
// OK - Here the right object was set, then, it is not reference problem:
grounds.transform.localScale = groundBounds.size / 10f;
// OK - Old value is correct:
Debug.Log(grounds.GetComponent<MeshRenderer>().material.GetTextureScale("_MainTex"));
// OK - Setting the value here:
grounds.GetComponent<MeshRenderer>().materials[0].SetTextureScale("_MainTex", groundBounds.size.xz());
// OK - New value is showing:
Debug.Log(grounds.GetComponent<MeshRenderer>().material.GetTextureScale("_MainTex"));
// OPS! Visually and in the inspector, the old value is still there, unchanged

What did I expect? I expect it to set the texture scale/tiling when my function executes (on Start method).

What is happening? The value is getting changed, but visually and in the inspector the value remains the same unchanged value!

Further information:

  • Unity 2019.1.4f1 with Lightweight Render Pipeline running on Windows 10 (build target = PC);

  • I have found through many topics across the web that it is one of the possible ways to set the texture;

  • I have tested with:

  • .materials[0] instead of “material”

  • material.mainTextureScale instead of material.SetTextureScale

You’re modifying a copy of the data. Per the docs: Note that like all arrays returned by Unity, this returns a copy of materials array. If you want to change some materials in it, get the value, change an entry and set materials back. It’s a common paradigm in Unity so something good to learn early.

It didn’t do the trick, but thank you any way! I understand it returns a copy of the materials array (in fact, I was unaware of that), but it seems to return the Material itself. We should be refering to the Render.material docs instead.

Now I understood that using the Lightweight Render Pipeline is actually not compatible with this way of setting material properties.

I replaced the LWRP Lit shader by the Standard shader. The object got pink, of course, but the values were changed. When I replaced it back to the LWRP Lit shader, the object was texturized, but the values didn’t change.

Now, I don’t know what to set in the material to get the changes, once “_Color” and “_MainTex” seems not to be available in LWRP shader.

Problem solved! According to https :// forum . unity . com/threads/meshrenderer-material-setcolor-does-not-change-color-lwrp.583978/ (why can’t I post the referenced link?) they have changed the names. “_Color” were renamed to “_BaseColor” and “_MainTex” seems to be “_BaseMap” now. I am a little angry they didn’t put it into an obvious way, it costed time to res, but I am happy the problem was solved!