How to set base/tint color of material.

using UnityEngine;
using System.Collections;

public class Sidewalls_Init : MonoBehaviour {
    public Material SidewallMaterial;
    void Update() {
        SidewallMaterial.color = Color(255, 255, 255, 1);
    }
}

Color is “errored”, saying, quotation:
Assets/Scripts/Sidewalls/Sidewalls_Init.cs(7,34): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected.
Knowing my knowledge it should be #FFFFFF, 100% opacity.

You’ve got it right, but there’s a little JS in your syntax. Change Color to new Color and that should work. Also if you wanna get the material from a particular object that this script will go on, you can do something like…

Material m = gameObject.getComponent().material;

And then go on with your above code. It’s a great way to have strobe-like tints on your textures :stuck_out_tongue:

1 Like

I used my code and corrected it, a.k.a. Color(); to new Color();.

  • Changed:
    “SidewallMaterial.color = Color(255, 255, 255, 1);”
    Into
    “SidewallMaterial.color = new Color(255, 48, 48, 1);”

  • Sidewall (item (cube))
    Assigned script “Init_Sidewall” (script provided above)
    Assigned material “Empty Material” (white base color)
    Assigned material “Empty Material” to slot “Sidewall Material”.

  • Empty Material
    Assigned base color, white.

This color doesn’t change to red-ish, could you maybe help me? :slight_smile:

Sure. A few things… First, change Update to Start. This operation doesn’t appear to be needed every frame of your game :wink: Second, you can get the material being used by your object with the code I listed above. And lastly, when you edit materials, it turns them into their own instance of that material. SidewallMaterial isn’t connect to your object, it’s just defined in the class on your object. They are the same material until you edit it.

No matter how you go about this, you’ll at some point need to access the mesh by getting it off your object. Here’s a few options for you:

/*option one - directly access and change the material*/
void Start()
{
    gameObject.getComponent<MeshRenderer>().material.color = new Color(255,48,48,1);
}

/*option two - using a temporary variable*/
void Start()
{
    Material m = gameObject.getComponent<MeshRenderer>().material;
    m.color = new Color(255,48,48,1);
    /*This might not be necessary, I forget.*/
    gameObject.getComponent<MeshRenderer>().material = m;
}

/*option 3 - using your existing variable*/
void Start()
{
    SidewallMaterial.color = new Color(255,48,48,1);
    gameObject.getComponent<MeshRenderer>().material = SidewallMaterial;
}

I recommend any of them. Using a temporary variable might be the best for future cases where you don’t need a reference to the material often, but when you do you will have that temporary variable to do multiple things to.

1 Like

Pssssst. Your code produces error, apparently “getComponent” in each case needs to be “GetComponent”. :slight_smile:

I must have misplaced scripts or something like this. I see how these codes would work, normal logic, but they don’t on my. I apply this script on “Sidewall” element, the “Sidewall” has “Empty Material” in it and “Empty Material” is also in slot.

Lol sorry about GetComponent, I usually let code completion take the wheel on capitalization.

I suggest trying option 1 just to see if it works. If that doesn’t work, you have a more unique problem.

1 Like

Then I have unique problem, it still doesn’t work. I’m an idiot xD

Try running this code in a start function in any script attached to your object:

Debug.Log(gameObject.GetComponent<MeshRenderer>() == null);
Debug.Log(gameObject.GetComponent<MeshRenderer>().material == null);
Debug.Log(gameObject.GetComponent<MeshRenderer>().material.color);

That should make the error clear, if there is one. You should see False, False and then color data. If you see True for either of the first two, your object’s mesh renderer is bugged lol.

1 Like

Hey Tom,

I attached these three commands to the currently attached script, now it looks like this:

using UnityEngine;
using System.Collections;

public class Sidewalls_Init : MonoBehaviour {
    public GameObject Sidewall_Object;
    void Start() {
        Sidewall_Object.GetComponent<MeshRenderer>().material.color = new Color(255, 18, 18, 1);

        Debug.Log(gameObject.GetComponent<MeshRenderer>() == null);
        Debug.Log(gameObject.GetComponent<MeshRenderer>().material == null);
        Debug.Log(gameObject.GetComponent<MeshRenderer>().material.color);
    }
}

Results:

gameObject.GetComponent() == null
False

gameObject.GetComponent().material == null
False

gameObject.GetComponent().material.color
RGBA(255.000, 18.000, 18.000, 1.000)

Couple notes:
When game is not started, block appears gray.
When game is started, block appears white.
and I still don’t even.

Maybe I could provide you entire project, in case this would be remotely unresolvable, if you find time.

This is what I use in my christmas goof project.

The Owner is a playmaker thing, this is a script I made to be used with playmaker. Just switch Owner to gameObject.

1899069–122403–RandomizeMaterials2.cs (527 Bytes)

Psssst… “MonoBehavior” has to be “MonoBehaviour” :).

False, false, RGBA(0.071, 0.071, 0.071, 1.000). I’ve put it in the same Sidewall object the previous script sat in, it produced this result, removing previous script, remained exact result. Switching “Owner” to “gameObject” or “GameObject” won’t work, produces some kind of error, used “this.name” instead, couldn’t put anything.

I was editing that script in notepad++, so it didn’t catch any of that xD

So if you disable every script on the object and run only 1 that modifies the material tint, it does nothing? Maybe you could provide screenshots or something.

“So if you disable every script on the object and run only 1 that modifies the material tint, it does nothing?”
The only script that we talked about in this topic, is the only script EVER attached to the object.

“Maybe you could provide screenshots or something.”
Heh, I started new conversation between us, I also sent link with entire project (it’s just couple files of my own), not really big, in fact, it’s quite basic. Because I don’t quite understand how screenshot would help? Script is attached to item, item doesn’t change color with in-the-face command :). You would see, exactly what I’m telling, heh, maybe video would be efficient but with my upload speed, it would be hardcore pain in the back.

Ok I’ll take a look