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 () {
}
}
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;
}
}