In my game there is a powerup that allows the player to go through walls. When they get this powerup, I want the walls to become semi-transparent. I know you can do this by changing a GameObject’s material’s shader to Transparent/Diffuse, but how would you do this with code, while still keeping the same texture of the object?
Under my “Renderer” component, in the Materials section, there is no shader option. So wouldn’t this code not work? All it has under it is “grid pattern 02” (my material). Grid pattern 02 is also the component-material under the object.
First you should create 2 materials. First is common wall material:
Second is transparent wall material: all same, but rendering mode is Fade and alpha channel is lower:
After this you should just place script for changing this materials.
using UnityEngine;
using System.Collections;
public class MaterialReplace : MonoBehaviour {
public Material transparentMaterial;
bool isTransparent = false;
float lastTime;
Material previousMaterial;
void Start () {
lastTime = Time.time;
previousMaterial = gameObject.GetComponent<Renderer>().material;
}
void Update(){
if (Time.time - lastTime > 3) {
lastTime = Time.time;
if (!isTransparent) {
gameObject.GetComponent<Renderer>().material = transparentMaterial;
isTransparent = true;
} else {
gameObject.GetComponent<Renderer>().material = previousMaterial;
isTransparent = false;
}
}
}
}
Done! Now materials will replace every 3 secounds!
Good luck in your project!