My gun uses an overheating system rather than ammo. To relay this to the player, I have a red light and a green light on the gun. I’m trying to change the opacity of the lights based on the heat level. So far, I have the red color changing perfectly in the script, it just doesn’t change the actual material in-game. I’ve tried everything I can thing of and searched Ask Unity for ages but haven’t found a solution. Here’s the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SimpleShoot : MonoBehaviour
{
public GameObject bulletPrefab;
public GameObject casingPrefab;
public GameObject muzzleFlashPrefab;
public Transform barrelLocation;
public Transform casingExitLocation;
private bool Cooldown;
private bool NeedsToCharge;
public bool Fire;
public float Delay;
public float RechargeTime;
public float Heat;
public float HeatToIncrease;
public float speed;
public float shotPower = 100f;
private Material Green;
private Material Red;
public GameObject GreenMesh;
public GameObject RedMesh;
public Color GreenColor;
public Color RedColor;
void Start()
{
Cooldown = true;
Green = GreenMesh.GetComponent<SkinnedMeshRenderer>().material;
Red = RedMesh.GetComponent<SkinnedMeshRenderer>().material;
if (barrelLocation == null)
barrelLocation = transform;
}
void Update ()
{
if (Fire == true)
{
Shoot();
}
if (Heat > 1)
{
StartCoroutine(Recharge());
}
RedColor.a = Heat;
if (Red.color != RedColor)
{
Red.color = Color.Lerp(Red.color, RedColor, Time.deltaTime * speed);
}
}
public void StartShoot ()
{
GetComponent<Animator>().SetBool("Fire", Fire);
}
void Shoot()
{
if (Cooldown == true && NeedsToCharge == false)
{
StartCoroutine(ShootDelay());
}
}
void CasingRelease()
{
GameObject casing;
casing = Instantiate(casingPrefab, casingExitLocation.position, casingExitLocation.rotation) as GameObject;
casing.GetComponent<Rigidbody>().AddExplosionForce(550f, (casingExitLocation.position - casingExitLocation.right * 0.3f - casingExitLocation.up * 0.6f), 1f);
casing.GetComponent<Rigidbody>().AddTorque(new Vector3(0, Random.Range(100f, 500f), Random.Range(10f, 1000f)), ForceMode.Impulse);
}
public IEnumerator ShootDelay ()
{
Cooldown = false;
yield return new WaitForSeconds(Delay);
GameObject tempFlash;
Instantiate(bulletPrefab, barrelLocation.position, barrelLocation.rotation).GetComponent<Rigidbody>().AddForce(barrelLocation.forward * shotPower);
tempFlash = Instantiate(muzzleFlashPrefab, barrelLocation.position, barrelLocation.rotation);
Heat = Heat + HeatToIncrease;
Cooldown = true;
}
public IEnumerator Recharge()
{
NeedsToCharge = true;
yield return new WaitForSeconds(RechargeTime);
Heat = 0;
NeedsToCharge = false;
}
}
Extra info: I’m using Nokobot’s modern handgun asset from the Asset Store. It’s a VR game so displaying the heat meter via UI is off the table.