So I’ve got a script attached to a prefab. When the mouse is clicked the prefab instantiates and the clone is supposed to move and fade.
Except when the prefab instantiates it just disappears. Works fine if I comment out the colour change, but the issue is definitely with spriteRenderer.
Also works fine if I just place the prefab on the scene, but doesn’t work when I drag and drop the prefab during play.
Anybody run into this issue with sprites and colour changing? Any ideas appreciated.
using UnityEngine;
using System.Collections;
public class clickGood : MonoBehaviour
{
public Color A = Color.white;
public Color B = Color.clear;
public float colorChangeSpeed;
SpriteRenderer spriteRenderer;
public Transform currentPosition;
public Vector3 newPosition;
private float startTime;
public float floatDistance;
public float speed;
void Start()
{
//colour change
spriteRenderer = GetComponent<SpriteRenderer>();
//move
startTime = Time.time;
currentPosition = gameObject.transform; ;
newPosition = new Vector3(transform.position.x, transform.position.y + floatDistance, transform.position.z);
}
void Update()
{
//colour change
spriteRenderer.color = Color.Lerp(A, B, Time.time * colorChangeSpeed);
//move
float currentDuration = (Time.time - startTime) * speed;
float journeyFraction = currentDuration / floatDistance;
transform.position = Vector3.Lerp(currentPosition.position, newPosition, journeyFraction);
if ((currentDuration / speed) >= floatDistance)
{
Destroy(gameObject);
}
}
}
I had a similar problem. I solved it by resetting the color in OnEnable() because of how I’m pooling my objects. But for you it could probably work in Awake()
void OnEnable()
{
// Make sure to reset the color
s_renderer = GetComponent<SpriteRenderer> ();
if (s_renderer.color == B) {
s_renderer.color = A;
}
// reset timer variables
isDissasembling = false;
isFading = false;
}
Also, if you have trouble controlling the duration of the fade using Time.time check out this other post that talks about setting your own time variable. Hope this helps.