- Home /
Change material during runtime?
I've been working on a small game that could be described as a bit of a crossover between Pacman and Flower. What I want to do is that as the player drives past buildings in the environment, said buildings light up. What I've been doing until now is that I replaced the prefabs, but this doesn't seem to be a particularly efficient way of doing so. So what I tried to do after that was to change the Emission parameter through code, something that Unity doesn't seem to support particularly well. So another idea I have now is to swap out the material entirely. But yet again, I don't know how to do so.
My code looks as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IlluminScript : MonoBehaviour {
public Material LitMaterial;
// Use this for initialization
void Start () {
}
void OnTriggerEnter(Collider col) {
if (col.gameObject.CompareTag ("ActiveZone")) {
}
}
// Update is called once per frame
void Update () {
}
}
Answer by OneCept-Games · Jan 03, 2018 at 08:34 PM
If you want to replace the Material on a Mesh (3D Model), you can get the MeshRenderer component from your object, and provide a new material in your OnTriggerEnter callback:
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour {
public Material material1;
public Renderer rend;
void Start() {
rend = GetComponent<Renderer>();
}
void Update() {
if (Input.GetButtonDown("Jump"))
rend.material = material1;
}
}
I tried this solution and it seems to work, although when I use my trigger system, nothing happens :/
Do you have a collider component on your object?
Answer by Ginxx009 · Jan 04, 2018 at 01:48 AM
Try this extension method
public static void ChangeMaterial(this GameObject go, Material mat)
{
go.renderer.material = mat;
}
Then you can call it by using
if(hit.collider.gameObject.tag == "Colour1")
{
hit.gameObject.renderer.material.ChangeMaterial( Your Material );
}
Here is another extension method for changing the color
public static void ChangeColor(this Material mat, Color color)
{
mat.SetColor("_Color", color);
}
method that supports multi materials on the gameObject
public static void ChangeColor(this GameObject go, Color color)
{
if(go.GetComponent<MeshRenderer>())
{
Material[] materials = go.gameObject.renderer.materials;
foreach(Material m in materials)
{
m.SetColor("_Color", color);
}
}
}
Finally to be more specific to your problem here is one final function for you
public static void ChangeColor(GameObject[] gameObjects, Color color)
{
foreach(GameObject gameObject in GameObjects)
{
gameObject.renderer.material.ChangeColor( "Your Color" );
}
}
then you can call it like this:
if(hit.collider.gameObject.tag == "Colour1")
{
GameObject[] _Colums = GameObject.FindGameObjectsWithTag("column");
ChangeColor(_Colums, "Your Color");
}
Here the source . Hope it helps :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612145747im_/https://answers.unity.com/themes/thub/images/avi.jpg)