Search Unity

Change material shader in game

Discussion in 'Shaders' started by startas, Dec 17, 2014.

  1. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    I want to change material shader in game, like from tessellation shader to a simple diffuse shader, i use this material as my ground texture. Now, when i try to play my game in unity editor, shader changes correctly, but when i build a separate pc game, my texture change from tessellated to plain pink color. Piece of code :

    Code (JavaScript):
    1. /*In some function*/
    2. if(Input.GetKeyDown(KeyCode.V)) {
    3.     if(tessellation) {
    4.       ChangeTextures("Bumped Diffuse");
    5.       tessellation = false;
    6.     }
    7.     else {
    8.       ChangeTextures("Tessellation/Bumped Specular (displacement)");
    9.       tessellation = true;
    10.     }
    11.   }
    12.  
    13. function ChangeTextures(name : String) {
    14.   var obj : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
    15.   for(var a : GameObject in obj) {
    16.     if(a.name == "Earth Plain") {
    17.       a.GetComponent(MeshRenderer).material.shader = Shader.Find(name);
    18.     }
    19.   }
    20. }
    So, what is wrong, whats happening in my game ? Is it not able to change a shader properly, or maybe material textures gets reseted and i need to set textures again after changing shader ?
     
  2. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    Never mind, managed to do it by creating global array of Material, then creating some materials, assigning materials to array through unity Inspector, and then just changing in code a.getblaah.material = matsArray .