Changing alpha in update for gameobject and particles

Hello! I’ve been trying for a few hours to get a little “fade” effect after picking up a coin (just before the “Destroy (gameObject)”).

Looking here and there I found something that, at first I thought it would work, but by deactivating the if that controls when it has reached a certain alpha level, I see that it certainly does not work.

Could someone guide me a little?

Thank you very much.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Coin : MonoBehaviour {

    public static int coinsCollected = 0;
    public static int coinNeededForWin = 10;
    public AudioClip clip; // coin collected sound
    public ParticleSystem particle; // particle when we take the coin
    private bool coinAlreadyCollected = false; // just for not take the coin two times, just in case.
    private Color alphaColour; // alpha for the coin
    private Color alphaColourParticle; // alpha for the particle


    void Awake()
    {
        alphaColour.a = gameObject.GetComponent<Renderer> ().material.color.a;
        alphaColourParticle.a = particle.GetComponent<Renderer> ().material.color.a;
    }



    void Update()
    { 
        this.transform.Rotate(1f, 1f, 2f);

        if (coinAlreadyCollected == true)    //Let's say goodbye to the coin collected.   
        {
            alphaColour.a -= 0.05f;
            alphaColourParticle.a -= 0.05f;
            if (alphaColour.a <= -2.0f){Destroy (gameObject,0.1f);}
        }

    }
// More things unrelated
}

Currently at work so I can’t dive in too deep for you on this, but in the Update method, it looks to me like you’re changing a Color property value for your component, but not assigning it back to the particles in your ParticleSystem.

Instead of just setting the alphaColour.a and alphaColourParticle.a in your Awake, try setting references to the actual particles. Then in Update when doing your subtraction of the alpha values, reassign the color back to the particles.

2 Likes

Thanks Schneider21. I did it bad too. It is a bit hard to understand that the references and how to manage them.

After fix it, it still didn’t work BUT this knowledge show me the way to find more specific information, and gotcha!

The answer was given by jmasinteATI in that message and the screnshot posted.

Anyway still trying find the way to apply it to the particles. I guess I must edit the prefab that I taken from Store and check the material too.

Thanks again Schenider21!