Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Tint a mainTexture image

Discussion in 'Scripting' started by ds223, Nov 5, 2018.

  1. ds223

    ds223

    Joined:
    Nov 5, 2018
    Posts:
    8
    I am trying to tint an image, here is my code:
    Code (CSharp):
    1. public Texture2D image;
    2.  
    3. GetComponent<MeshRenderer>().material.shader = Shader.Find("Unlit/Texture");
    4. GetComponent<MeshRenderer>().material.mainTexture = image;
    5. GetComponent<Renderer>().material.color = new Color(1, 0, 0, 0);
    If I just have this code it does tint the image but it is dark:
    Code (CSharp):
    1. GetComponent<MeshRenderer>().material.mainTexture = image;
    2. GetComponent<Renderer>().material.color = new Color(1, 0, 0, 0);
    Thanks
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    why are you setting the alpha to zero? it mean transparent
     
  3. ds223

    ds223

    Joined:
    Nov 5, 2018
    Posts:
    8
    Was trying anything, setting it to this does the same thing:

    Code (CSharp):
    1. Color(1, 0, 0);
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    and if you do
    Code (CSharp):
    1. Color(1f, 0f, 0f, 1f);
    this should be solid red, does it do something in the inspector?
    any thing showing up in the console?
     
  5. ds223

    ds223

    Joined:
    Nov 5, 2018
    Posts:
    8
    Hopefully this shows the problem better:

    See attached images

    with shader.png is running this:
    Code (CSharp):
    1. GetComponent<MeshRenderer>().material.shader = Shader.Find("Unlit/Texture");
    2. GetComponent<MeshRenderer>().material.mainTexture = image;
    3. GetComponent<Renderer>().material.color = new Color(1f, 0f, 0f, 1f);
    without shader.png is running this:
    Code (CSharp):
    1. GetComponent<MeshRenderer>().material.mainTexture = image;
    2. GetComponent<Renderer>().material.color = new Color(1f, 0f, 0f, 1f);
    without shader and no red.png is running this:
    Code (CSharp):
    1. GetComponent<MeshRenderer>().material.mainTexture = image;
    Notice how "without shader and no red.png" is darker than "with shader.png"
     

    Attached Files: