Search Unity

Unable to gettexture from propertyblock

Discussion in 'Scripting' started by pyjamaslug, Feb 1, 2021.

  1. pyjamaslug

    pyjamaslug

    Joined:
    Jul 5, 2017
    Posts:
    51
    I want to get the texture from a material already assigned to _rend1 and pass it to an override material via _rend2. I can do it by accessing the materials directly but have so far failed when I try to use propertyblocks. (renderers are assigned in the inspector)

    Code (CSharp):
    1.   MaterialPropertyBlock propBlock1 = new MaterialPropertyBlock();
    2.   _rend1.GetPropertyBlock(propBlock1,0);
    3.   Texture gotTexture = propBlock1.GetTexture("_BaseColorMap");
    4.  
    5.   MaterialPropertyBlock propBlock2 = new MaterialPropertyBlock();
    6.   _rend2.GetPropertyBlock(propBlock2,0);
    7.   propBlock2.SetTexture("_BaseColorMap", gotTexture);
    8.   _rend2.SetPropertyBlock(propBlock2,0);
    9.  
    This produces an error where gotTexture is flagged as null when I use it in SetTexture() on propBlock2.
    If I set a new texture on propBlock1 and then read it back, that works fine. What do I need to do to get the original texture from propBlock1?