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. Dismiss Notice

Unable to change material color at runtime

Discussion in 'Scripting' started by hgokturk, Dec 25, 2018.

  1. hgokturk

    hgokturk

    Joined:
    Jun 29, 2016
    Posts:
    17
    material.color or material.SetColor does not work. Objects is a pool of Gameobjects(prefab) Tried mobile-diffuse and mobile-particles-alpha blended.

    Code (CSharp):
    1. Objects[PoolSize].GetComponent<Renderer>().material.color = Color.cyan;
    Primary goal is to be able to change alpha of material's color. What is causing it?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @hgokturk

    Hi. Your material shader should be such it supports transparency. Then you change it's material's color:

    Code (CSharp):
    1. var col = new Color(1f,0f,0f,0.5f);
    2. GetComponent<Renderer>().material.color = col;
    You'll have to change the color and alpha at once, as it's a struct and then re-assign it. Here I made the color red and alpha 50% transparent.

    Also, you probably don't want to change colors of prefabs, but instantiated clones of those while in play mode.
     
  3. hgokturk

    hgokturk

    Joined:
    Jun 29, 2016
    Posts:
    17
    @eses Yeah, that is actually clones of prefabs that i'm trying to change the color/alpha of. So i must find a good mobile shader which allows me to change the color right?
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @hgokturk

    I don't remember if there is a transparency supporting mobile shader, but at least you can test this by setting your Material to use Standard shader and set it's Rendering mode to Transparent. Maybe someone else can help you with the shader.