[Solved]Noob Q's regarding setting color of material

Hi all,

Firstly, can i say I’ve search the forums for this and have found many threads but i still can’t get it to work…

I am trying to alter the color of a material via code (i am using c#).

I have done the following but I’m not sure what to do now:

void Update () {
        renderer.material.color.r = 1.0;        
	}

I have also tried and similars but to no effect:

renderer.material.SetColor ("_TintColor", Color);

Can anyone help as i’m new to scripting.

Also, I saw an example where the first line of code was:

myGameObject.renderer.material.color = Color.red;

If I create a script and add it to a specific gameobject, can i omit the myGameObject section and do i need to reference the material within the script or will ‘dragging it onto’ the gameObject sort this?

Apologies for the novice questions!!

Matt.

I think I’ve solved the problem with:

private Color col = new Color(1,0,0,1);
 	// Use this for initialization
	void Start () {
        renderer.material.color = col;
	}
       
	// Update is called once per frame
	void Update () {
        col.r = (float)0.0;
        col.g = (float)1.0;
        col.b = (float)0.5;

        renderer.material.color = col;
	}

Well, I’ve managed to alter the color so that’s a start.

Cheers,
Matt.

instead of putting (float) before your number, type an F. C# needs it to recognize floating point numbers :slight_smile:

private Color col = new Color(1,0,0,1);
    // Use this for initialization
   void Start () {
        renderer.material.color = col;
   }
       
   // Update is called once per frame
   void Update () {
        col.r = 0.0F;
        col.g = 1.0F;
        col.b = 0.5F;

        renderer.material.color = col;
   }