Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to Change Tint of Specific GameObject without Multiple Materials

Discussion in 'General Graphics' started by ezjm427, Mar 14, 2017.

  1. ezjm427

    ezjm427

    Joined:
    May 17, 2014
    Posts:
    100
    Hi, as it says I'm not sure how I should change the tint of a GameObject without changing it for all other GameObjects using the same material. The easiest way of changing a tinting color of an object is by changing the material's color, but the problem is this changes it for all GameObjects that are using the material.

    Multiple duplicate materials is one solution, but for a large or variable number of GameObjects I don't see this working. Any ideas?
     
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    You could always clone the material at runtime.
     
    ezjm427 likes this.
  3. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    I believe a color mask shader is what you are looking for
     
  4. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    The issue is the material is being used by multiple objects, so changing the color mask is going to effect all objects using that material (unless a copy is made).
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    You can use a script to modify the material on each Renderer at runtime, either by setting the value directly on the Renderer.material (which auto instantiates a new material) or using a MateriaPropertyBlock (which does not create a new material, and can be used with instancing), or by using vertex colors via AdditionalVertexStreams and a shader that uses that vertex color (which works well with batching).
     
    BlazeDrake, ezjm427 and larku like this.
  6. ezjm427

    ezjm427

    Joined:
    May 17, 2014
    Posts:
    100
    Thanks, this'll work, didn't think it would be that simple because some things with Unity are confusing whether properties are readonly, or just creating copies when referencing them in code, or if you are actually accessing the property.
     
    Last edited: Mar 24, 2017
    larku likes this.