Search Unity

Set Alpha to Transparency in Runtime with a script

Discussion in 'Scripting' started by PandawanFr, May 23, 2015.

  1. PandawanFr

    PandawanFr

    Joined:
    Nov 27, 2013
    Posts:
    68
    Hello!

    I was wondering if there is a way to change a texture's alpha to transparent (imported in runtime).
    Basically, I am making a system to change textures, and I want it so that when applying a texture with Alpha, set the alpha to transparent, right now I see it black. I already have the texture system. I have searched a little bit on Google and I can only find things about setting Alpha to Transparency but not in runtime (I can only find for editor). Which I already know.


    Thank you!
     
    Last edited: May 23, 2015
  2. PandawanFr

    PandawanFr

    Joined:
    Nov 27, 2013
    Posts:
    68
    For a bit more info:

    The script uses a simple.
    Texture newTexture;

    Which I then search in a folder for a certain texture and set it.
    And I just basically want to set Alpha to transparency before this:

    Renderer rend = gameObject.GetComponent<Renderer> ();
    rend.material.mainTexture = newTexture;

    In the editor, it would be like that:

    But I want to do it in a script (C# if possible).
     
  3. PandawanFr

    PandawanFr

    Joined:
    Nov 27, 2013
    Posts:
    68
    Ok, found it after a lot of searches, I thought I needed to modify the texture, but it's the material/shader that matters.

    Here's the code:
    Code (CSharp):
    1. Renderer rend = gameObject.GetComponent<Renderer> ();
    2. Shader shader = Shader.Find("Transparent/Diffuse");
    3. rend.material.shader = shader;