Search Unity

Dynamic Texture change on Skinned Mesh

Discussion in 'General Graphics' started by Tempy111, Jan 18, 2022.

  1. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    I'm trying to change the texture on a Mesh at run time but it doesn't want to work.. well.. it IS doing something but..

    I have the player character which is a game object, which has the mesh inside it. That mesh has 'Skinned Mesh Renderer', under which it has the texture used (only one) as Element 0 under Materials. It also seams to have the Material a bit later which is the same thing.. I've created another material which has a texture.

    The code i'm using to switch the texture is pretty simple and is:

    Code (CSharp):
    1. void MaterialUpdate(){
    2.         Material BlinkFrame = Resources.Load<Material>("BunnyTex_Blink");
    3.         Mesh mesh = GetComponent<Mesh>();
    4.         SkinnedMeshRenderer meshRenderer = GetComponentInChildren<SkinnedMeshRenderer>();
    5.         meshRenderer.material = BlinkFrame;
    6.     }
    where the new material is called 'BunnyTex_Blink", and it's attached to the Game Objecter.

    If i attach the material manually in the editor, it's fine and maps no problem, but when doing this ingame via this code, it just turns the mesh bright pink/purple and drops all lighing and shadow from the mesh, so i'm clearly doing something wrong but looking online, most of what i find either isn't working out or the editor moans it can't do
     
  2. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    ugh, I'm an idiot.. I had a misspelling in the Resources folder name so it couldn't find it.