I have an splatter effect I am trying to implement when an enemy is killed with the following script:
private var splatFadeRate : float = 0.33;
private var splatRunRate : float = 0.5;
private var splatDeathRate : float = 2.75;
private var startColor : Color;
private var endColor : Color;
function Start ()
{
startColor = renderer.material.color;
endColor = Color (startColor.r, startColor.g, startColor.b, 0.0);
Debug.Log ("[Start] Alpha = " + startColor.a);
}
function Update ()
{
if (transform.localScale.z < 1.0)
{
transform.localScale.x = Mathf.Lerp (transform.localScale.x, 1.0, Time.time);
transform.localScale.z = Mathf.Lerp (transform.localScale.z, 1.0, Time.time);
}
if (transform.localScale.z >= 1.0)
{
transform.position.y = Mathf.Lerp (transform.position.y, transform.position.y - 3.0, Time.deltaTime);
transform.localScale.z = Mathf.Lerp (transform.localScale.z, transform.localScale.z + 1.0, Time.deltaTime * splatRunRate);
renderer.material.color = Color.Lerp (startColor, endColor, Time.time * splatFadeRate);
Debug.Log ("[Update] Alpha = " + renderer.material.color.a);
}
Destroy (gameObject, splatDeathRate);
}
I invoke it in my enemy’s script as follows:
var splat : GameObject = Instantiate(splatPrefab, Vector3(this.transform.position.x, this.transform.position.y, -2), Quaternion.identity);
The problem is that the splatter effect works the first time it is instantiated (game object in hierarchy or instantiated for first round of enemies), but then never displays again upon subsequent instantiations (2nd, 3rd, etc… rounds of enemies, drag and drop from project view to hierarchy). I’ve verified that the alpha jumps from 1.0 to 0.0, seemingly without executing Color.Lerp. I’ve verified that the splatter prefab to which the splatter effect script is attached does indeed instantiate, but the texture/material will not display.
I have a sneaking suspicion I’m effecting the material’s alpha globally, not locally. Any suggestions on correcting this problem?