Douglas William
Douglas William

Reputation: 113

Change Renderer color

I am new to unity engine

how to put color on the cube ?

using UnityEngine;
using System.Collections;

public class cor_cube : MonoBehaviour {

public Color colorStart = Color.red;
public Color colorEnd = Color.green;
public float duration = 1.0F;
public Renderer rend;
void Start() {
    rend = GetComponent<Renderer>();
}
void Update() {
    float lerp = Mathf.PingPong(Time.time, duration) / duration;
    rend.material.color = Color.Lerp(colorStart, colorEnd, lerp);
    }
} (I don't want like that)

I want a fixed color.

Upvotes: 0

Views: 2395

Answers (2)

Programmer
Programmer

Reputation: 125245

I want a fixed color.

It looks like you don't want to lerp from one color to another. You just want to set the color.

Below are the ways to change color:

Method 1: Predefined Colors

void Start() {
    rend = GetComponent<Renderer>();
    rend.material.color = Color.blue;
}

There many other predefined Colors such as Color.blue,Color.white,Color.black,Color.green and Color.gray.

Method 2: Custom Color with RGB or RGBA values

You can also create a custom color that is NOT defined in the Color structure. The format is in RGBA(Red,Green,Blue,Alpha). The values are float numbers from 0.0 to 1.0.

Full Red Color rend.material.color = new Color(1,0,0,1);

Full Blue Color

rend.material.color = new Color(0,1,0,1);

Full Green Color

rend.material.color = new Color(0,0,1,1);

Half Way Visible Blue Color

rend.material.color = new Color(0,1,0,0.5f);

Hidden Blue Color

rend.material.color = new Color(0,1,0,0);

Method 3: HSV Color

rend.material.color = Color.HSVToRGB(0,0,0);

I can't go on and on but this should get you started. Make sure to create a material in Editor and assign it to the cube so that the default one wont be used. You only need to create one and use it for all your cubes. When you change the color, a new material will be created for you. If you don't want that, use rend.sharedMaterial.color instead of rend.material.color.

In Unity 5, things changed. To get the alpha to change, you have to select the material and change the Rendering Mode from Opaque(default) to Fade or Transparent. So old tutorials may not work because of this.

Upvotes: 6

Dr Doodle
Dr Doodle

Reputation: 31

Create a material with the shader Unlit/diffuse on it. Set the texture to a plain white texture adn set the color of the material.

Upvotes: 0

Related Questions