[SOLVED] spriteRenderer not resetting on new clones

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.

Well I solved it eventually. Thanks for the link me_mania. The solution given by Shadowphax is the way to go.

Here is my final code.

using UnityEngine;
using System.Collections;

public class clickGood : MonoBehaviour
{
    //start colour and end colour
    public Color A;
    public Color B;
    SpriteRenderer spriteRenderer;
    //movement variables
    public Transform currentPosition;
    public Vector3 newPosition;
    private float startTime;
    public float floatDistance;
    public float speed;
    //IEnumerator for lerp
    public float duration = 1;
    public float smoothness = 0.02f;

    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        //initialize move variables
        startTime = Time.time;
        currentPosition = gameObject.transform;
        newPosition = new Vector3(transform.position.x, transform.position.y + floatDistance, transform.position.z);
        //lerp the sprite colour
        StartCoroutine("LerpColour");
    }

    void Update()
    {
        //move the sprite
        float currentDuration = (Time.time - startTime) * speed;
        float journeyFraction = currentDuration / floatDistance;
        transform.position = Vector3.Lerp(currentPosition.position, newPosition, journeyFraction);
        //kill the sprite
        if ((currentDuration / speed) >= floatDistance)
        {
            Destroy(gameObject);
        }
    }

    IEnumerator LerpColour()
    {
        //timer
        float progress = 0;
        float increment = smoothness / duration;

        while (progress < 1)
        {
            //lerp colour
            spriteRenderer.color = Color.Lerp(A, B, progress);
            //move time forward
            progress += increment;
            yield return new WaitForSeconds(smoothness);
        }
        yield return true;
    }

}