Change Renderer color - c#

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.

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.

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.

Related

How to add translucency gradient to a square from top to bottom?

I have no idea how to make an object translucent at the bottom but opaque at the top. My object is a 2D square.
I tried this but it didn't work:
using UnityEngine;
using UnityEngine.UI;
public class SquareColorChanger : MonoBehaviour
{
public Color startColor;
public Color endColor;
private Image _image;
private void Start()
{
_image = GetComponent<Image>();
}
private void Update()
{
float lerpValue = Input.GetAxis("Horizontal"); // this could be replaced with any other value that goes from 0 to 1
_image.color = Color.Lerp(startColor, endColor, lerpValue);
}
}
Lerping a color would change it over time. An image has only one Color value.
For what you want to to you either need to either put a gradient texture in the alpha channel which would be the easiest solution for this or create your own shader.
How to create gradient in shader graph you could follow this and make sure to put your gradient into alpha channel.

How can I set the color of a sprite with the URP Sprite-Lit-Default material in Unity?

I have an object that I upgraded to the URP Sprite-Lit-Default material, but since I did that, the code I wrote to set the alpha color doesn't work at all. Upon further testing, I realized that it doesn't seem to work with red, green, blue, or alpha.
Color c = rend.material.color;
c.a = 120;
rend.material.color = c;
In the code above, rend references my sprite renderer. What do I have to do to change the color values from code on the upgraded material? I would like to use some lighting effects, so I don't wanna just switch to something else that doesn't support URP 2D lighting.
Try this :
//Attach this script to any GameObject in your scene to spawn a cube and change the material color
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
//Create a new cube primitive to set the color on
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//Get the Renderer component from the new cube
var cubeRenderer = cube.GetComponent<Renderer>();
//Call SetColor using the shader property name "_Color" and setting the color to red
cubeRenderer.material.SetColor("_Color", Color.red);
}
}
https://docs.unity3d.com/ScriptReference/Material.SetColor.html

How to make color of object in unity 100% opaque

I set color alpha to 1.0f, i set shader to transparent/diffuse
color = new Color(255/255f, 0/255f, 0/255f, 1f) //unity scales color to 1 AFAIK
go.GetComponent<Renderer>().material.color = color;
go.GetComponent<Renderer>().material.shader = Shader.Find("Legacy Shaders/Transparent/Diffuse");
But i can still see original texture behind the red color. How can i paint it red whole, so i won't see the original texture?
Thanks
Can you set the material texture to null? Like this ->
go.GetComponent().materials[1].mainTexture = null;

Find and change property name in a shader

I want to change and set 3 things after setting the new material:
First The shader type to Unlit/Color
Second The albedo color to change it for example to: 255,0,0,255
Third The metallic value from 0 to 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorsLockManager : MonoBehaviour
{
public bool locked;
public Color lockedColor = Color.red;
public Color unlockedColor = Color.green;
public Renderer rend;
private GameObject[] doorPlanes;
private void Start()
{
doorPlanes = GameObject.FindGameObjectsWithTag("DoorPlane");
for (int i = 0; i < doorPlanes.Length; i++)
{
rend = doorPlanes[i].GetComponent<Renderer>();
if (locked)
{
rend.material.SetFloat("Metallic", 1);
rend.material.color = lockedColor;
}
else
{
rend.material.color = unlockedColor;
}
}
}
// Update is called once per frame
void Update ()
{
}
}
This line does nothing:
rend.material.SetFloat("Metallic", 1);
This is what I want to change:
When you need to change a shader property but you don't know what to use for the proper name to use, select the material, click on its settings icon then click on "Select Shader". From there, you will see all the shader property names and their types and you will know which function and name to use to change the shader's properties.
With the default standard shader it looks something like this:
You need to know this otherwise you would need to ask new question for each property you want to change.
Your Renderer reference:
public Renderer rend;
To set it, the SetXXX function is used:
rend.material.SetFloat("_Metallic", 1); //Metallic is a float
To get it the GetXXX function is used:
float metallic = rend.material.GetFloat("_Metallic"); //Metallic is a float
To change or get the The albedo color to 255,0,0,255.
rend.material.SetColor("_Color", new Color32(255, 0, 0, 255));
Color color = rend.material.GetColor("_Color");
Notice that I used Color32 instead of Color because Color32 takes values between 0 and 255 while Color expects values between 0 and 1.
To change the material's shader to "Unlit/Color", just find it then assign it to the material:
Shader shader = Shader.Find("Unlit/Color");
rend.material.shader = shader;
Note that the "Unlit/Color" shader doesn't have the _Metallic property. It has one one property and that is "_Color". You can use the first method I described to determine which properties it has before attempting to change them.
What if the shader is used on many different objects. I want all the
objects to change when the property is changed on one of them.
To change all the objects using the-same material, use Renderer.sharedMaterial instead of Renderer.material. The shared material changes the original material and every other objects should pick that new material up as long as you have not called Renderer.material on the material which actually makes a new copy for the said material and disconnects the renderer material from the original material.

Set material color with unity C#

I am working on a game in UNITY. For this game so far I have 3 cubes that I wish to target. I have them set up in an array and when I hit tab the targets switch between them based on distance. This is all fine but I have run into a pickle and it is that I wish to have the cubes I target turn red for visual representation. I seen on Youtube that people used the following line of code :
selectedTarget.renderer.material.color = Color.red;
However this does not work for me. I then seen a comment saying :
The function to render was deprecated. Below should work...
selectedTarget.GetComponent<Renderer>().material.color = Color.red;
This code does not work for me either. I get no errors mind you, it runs fine but the cubes do not turn red. Has anyone any idea on whether or not I am doing this right? I will post the entire script below and the code I am on about is in selectedTarget(). Any help would be appreciated and thank you!
private void SelectTarget(){
selectedTarget.renderer.material.color = Color.red;
}
I had the very very very same problem. I had no errors. You need to be able to set the color property on the material, and only CERTAIN shaders have this color property. I had to use for example Self-Illuminated/Bumped instead of say Mobile/VertexLit. Then changing the color should be fine as you can see a Main Color property in the editor/inspector.
Also make sure your mesh has Materials. If you don't have Materials, even if it's blank or a placeholder else it won't work! I'd create a texture preferably white colored and small like 5x5. Then attach the texture to your cube. After you attach it you can color it!
I've done cool stuff in my game with Color.Lerp, it'll fade from one color to the next! This example below ping pongs from white to red when the player is hit by an enemy indicating damage!
transform.renderer.material.color = Color.Lerp(Color.white, Color.red, Mathf.PingPong(Time.time * 3 * speedLerp, 1.0));
This is working for me.
using UnityEngine;
using System.Collections;
public class Tile : MonoBehaviour {
public Vector2 gridPos = Vector2.zero;
Renderer r;
public Color colorStart = Color.red;
public Color colorEnd = Color.green;
public float duration = 1.0F;
private float lerp;
// Use this for initialization
void Start () {
r = GetComponent<Renderer>();
lerp = Mathf.PingPong(Time.time, duration) / duration;
}
// Update is called once per frame
void Update () {
}
void OnMouseEnter()
{
r.material.color = Color.Lerp(colorStart, colorEnd, lerp);
//r.material.color = Color.black;
Debug.Log("X pos = "+ gridPos.x + "Y pos = "+ gridPos.y);
}
void OnMouseExit()
{
r.material.color = Color.Lerp(Color.white, Color.white,lerp);
}
}
set a bool type for color like
bool colorchange = false;
public void OnCollisionEnter (Collision col)
{
Debug.Log ("collide");
colorchange = true;
if (colorchange) {
transform.GetComponent<Renderer> ().material.color = Color.red;
}
}

Categories

Resources