Search Unity

Changing alpha in update for gameobject and particles

Discussion in 'Getting Started' started by vegetamaker, Mar 20, 2017.

  1. vegetamaker

    vegetamaker

    Joined:
    Feb 28, 2017
    Posts:
    37
    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.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Coin : MonoBehaviour {
    6.  
    7.     public static int coinsCollected = 0;
    8.     public static int coinNeededForWin = 10;
    9.     public AudioClip clip; // coin collected sound
    10.     public ParticleSystem particle; // particle when we take the coin
    11.     private bool coinAlreadyCollected = false; // just for not take the coin two times, just in case.
    12.     private Color alphaColour; // alpha for the coin
    13.     private Color alphaColourParticle; // alpha for the particle
    14.  
    15.  
    16.     void Awake()
    17.     {
    18.         alphaColour.a = gameObject.GetComponent<Renderer> ().material.color.a;
    19.         alphaColourParticle.a = particle.GetComponent<Renderer> ().material.color.a;
    20.     }
    21.  
    22.  
    23.  
    24.     void Update()
    25.     {
    26.         this.transform.Rotate(1f, 1f, 2f);
    27.  
    28.         if (coinAlreadyCollected == true)    //Let's say goodbye to the coin collected.  
    29.         {
    30.             alphaColour.a -= 0.05f;
    31.             alphaColourParticle.a -= 0.05f;
    32.             if (alphaColour.a <= -2.0f){Destroy (gameObject,0.1f);}
    33.         }
    34.  
    35.     }
    36. // More things unrelated
    37. }
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    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.
     
    vegetamaker and JoeStrout like this.
  3. vegetamaker

    vegetamaker

    Joined:
    Feb 28, 2017
    Posts:
    37
    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!
    http://answers.unity3d.com/questions/60453/fading-out-using-rendermaterialcolora-doesnt-work.html

    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!