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

[SOLVED]"Material doesn't have a color property '_Color

Discussion in 'Scripting' started by cj-currie, Sep 30, 2009.

  1. cj-currie

    cj-currie

    Joined:
    Nov 27, 2008
    Posts:
    337
    So I have a script that edits the alpha of an object. it works fine every time, but it frequently throws this error in the debugger: "Material doesn't have a color property '_Color'"

    Here is the script:

    Code (csharp):
    1. for (x in GetComponentsInChildren(Renderer)){
    2.         for (i in x.materials){
    3.             i.color.a = 0;
    4.         }
    5.     }
    It basically makes everything transparent, right? The material uses the Transparent/Diffuse shader, so it definitely has the "_Color" property. The script works perfectly according to what the player can see, but I don't like erroneous nullreferences. Anyone know if maybe this color property is initiated at some point after when the script would execute (runtime)?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, that wouldn't happen. I would assume that some of the materials you're accessing don't have a shader that uses the "_Color" property, as the error says. Although as far as standard shaders go, it would be hard to get one that doesn't have "_Color"...skybox and flare shaders being the only ones I can think of offhand. Try debugging it by printing the name of the materials that you're changing.

    --Eric
     
  3. cj-currie

    cj-currie

    Joined:
    Nov 27, 2008
    Posts:
    337
    Particle shaders don't have "_Color" either : P

    But materials with the Transparent/Diffuse shader definitely do. I checked. I also am using all kinds of checks to make sure it's not being accessed after the object is destroyed...but the warning is thrown before it's destroyed, while the effect is still working (changing the alpha of the material), and Debug.Log("About to set the colour", i : Material); doesn't highlight anything in the inspector. I also tried Debug.Log("About to set the colour", gameObject);
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Oh yeah, forgot about those. :)

    How about

    Code (csharp):
    1.       for (i in x.materials){
    2.          print (i.name);
    3.          i.color.a = 0;
    4.       }
    --Eric
     
  5. cj-currie

    cj-currie

    Joined:
    Nov 27, 2008
    Posts:
    337
    Aw, geeze, I found the problem and it was stupid.

    Talking about the Particle shader not having that "_Color" property made me realize that the particle system attached to the object also had a particle renderer on it, so GetObjectsInChildren(Renderer) was returning a material that indeed did not have that property. It was a simple fix from there.

    Thanks!
     
    na1-0a likes this.
  6. tota5

    tota5

    Joined:
    May 26, 2011
    Posts:
    6
    how did you solve it?

    Im facing the same problem, and i already knew that some of the renderers wouldnt have a color value, but i cant seem to find a way to check if the color exists or not, to set it or not!

    (ah, please, try always to post the solution along with the "hey! I solved this problem!" so other users can make use of your solution =D )
     
  7. giulio-pierucci

    giulio-pierucci

    Joined:
    Sep 4, 2012
    Posts:
    8
    You can try:

    using UnityEngine;
    using System.Collections;

    public class example : MonoBehaviour {
    void Example() {
    if (renderer.material.HasProperty("_Color"))
    renderer.material.SetColor("_Color", Color.red);

    }
    }

    from Unity Reference

    or in javascript if you prefer:

    // Attach this to a gameObject that has a renderer.

    if(renderer.material.HasProperty("_Color"))
    renderer.material.SetColor("_Color",Color.red);
     
    HelenL, glenneroo, betomaluje and 6 others like this.
  8. KYL3R

    KYL3R

    Joined:
    Nov 16, 2012
    Posts:
    134
    Just stumbled on this:
    Create ShaderGraph, create Texture2DAsset, name it "BaseColor" and convert to property. The default Reference is now "_BaseColor" which seems to throw this error:

    Material 'YourMaterial' with Shader 'Shader Graphs/YourShaderGraph' doesn't have a color property '_BaseColor'.

    It's gone since I renamed it.
     
    davidwhiffen and TheGamery like this.