Search Unity

Change shader of prefab

Discussion in 'Editor & General Support' started by jtsmith1287, Oct 30, 2014.

  1. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Currently some of my prefabs use meshes imported from blender. I can't modify the shaders of these objects for some reason, which I believe is preventing me from modify the alpha on the material.

    The current shader is Default-Diffuse.

    I'm trying to modify alpha via,

    Code (csharp):
    1.  
    2. Color color = gameObject.renderer.material.color;
    3. color.a = 0.5f;
    4. gameObject.renderer.material.color = color;
    5.  
    If I change r, g, or b I can modify the color of the object, just not the alpha. Not sure what's going wrong. I can't tell if it's the default diffuse that's my issue (which I'd like to change eventually anyway, so any help on that would warrant a 'woot') or if it's the code or ... meh ... I don't know!
     
  2. frankrs

    frankrs

    Joined:
    Aug 29, 2009
    Posts:
    300
    I dont think the alpha in the diffuse shader affects transparency if thats where you're going for.
     
  3. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    That is what I'm going for. How do I accomplish this?
     
  4. frankrs

    frankrs

    Joined:
    Aug 29, 2009
    Posts:
    300
    You need a to write a shader that does this. I'm not an expert in writing shaders but have had good results with the Strumpy Shader Editor as well as ShaderForge. I'd be willing to write one for you for a few dollars if you like.
     
  5. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    1) Are there no existing shaders that have alpha? I've seen stackoverflow and unity answers posts that explain how to just modify the color.a to change the alpha. They never said anything about writing a custom shader.

    2) I am still unable to switch out the shader of my objects. Anyone have a thought on why this is?
     
  6. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    972
    First of all, you need to make a new material and assign it onto the object. The reason you cannot change the shader is that it is currently using the default material.

    Then you should be able to try Transparent/Diffuse. You need to remember that that Transparent/Diffuse does not write to the depth buffer, so non-convex objects can potentially look weird. You can easily modify it (by grabbing the Unity Shader Sources) to write to the depth buffer (with ZWrite On), but you will then need to play with the render queue position the object is on. Any objects behind it that do a depth test and are rendered after it will fail their depth test. Leaving it in the transparent queue MIGHT be alright, try it out. Alternatively, if you have pro, you can use the stencil buffer to sort that out.

    There are examples of all these in the Unity Documentation.

    Enjoy!
     
    jtsmith1287 likes this.
  7. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Wow, killer. That's was perfectly in depth. Thanks!
     
    frankrs likes this.