How to change Material and its properties at runtime in Unity?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeMaterial01 : MonoBehaviour
{
public Material newMaterial; //You must drag your new material here in the inspector window
void Start () {

// Get the Mesh Renderer Component from this gameObject
MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
// Get the current material applied on this GameObject
Material oldMaterial = meshRenderer.material;
//print the material name in the console
Debug.Log("Applied Material: " + oldMaterial.name);
// Set the new material on the GameObject
meshRenderer.material = newMaterial;
}
}
public Material[] newMaterials;
{...
meshRenderer.materials = newMaterials;
...}
public Texture2D myTexture;private void Start()
{
GetComponent<MeshRenderer>().material.mainTexture = myTexture;
}
public Texture2D myTexture;private void Start()
{
MeshRenderer rend = GetComponent<MeshRenderer>();
rend.material.SetTexture("_MainTex", myTexture);
}
rend.material.GetTexture("_MainTex");
m_Renderer.material.EnableKeyword ("_METALLICGLOSSMAP");meshRenderer.material.SetTexture("_MetallicGlossMap", myTexture);
Color newColor = Color.blue;
GetComponent<MeshRenderer>().material. color = newColor;
MeshRenderer rend = GetComponent<MeshRenderer>();
rend.material. SetColor("_Color", Color.blue);
rend. material.SetFloat("_BumpScale", 0.5f);
Shader BumpedDiffuse = Shader.Find("Mobile/Bumped Diffuse"); 
GetComponent<MeshRenderer>().material.shader = BumpedDiffuse;

--

--

--

Computer Science Engineer and Cisco Instructor

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Display Products by Custom Attribute in Magento 2?

7 opinionated tools I always use to build my SaaS, and how I go to market faster.

DePay Payment Processor V1

Best Alternatives For SourceTree and Phpmyadmin on linux

Red Hat Summit April 20: Technical Sessions and Demos — That you should not Miss!

.NET CORE 3.1 Web API For Beginners. Part -01

What is Flutter?

Slices as arguments in Golang

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ouzani Abd Raouf

Ouzani Abd Raouf

Computer Science Engineer and Cisco Instructor

More from Medium

Working with Unity’s 2D Tilemaps

P4: Creating a Gritty Office Den

Making enemies go BOOM! Part 1 — Setup

Coding a Player Controlled Starship in Unity 2021