Search code examples
c#unity-game-engineunityscript

Fade out object


How i can fade out one object immediately after colliding with it?

I cant use Destroy (gameObject);because that object is a coin game with efx sound, if I destroy this once the object sound does not play when the player collides with the coin.

And if I do not destroy that, coin immediately, you earn points every time you bump this coin

I'm using Audio Mixer, so really need the sound of the coin comes from the Audio Source to can set the volume on my settings.

My idea:

void OnCollisionEnter2D(Collision2D colisor)
{
    if (colisor.gameObject.tag == "Bee") {

        GetComponent<AudioSource> ().Play();

        // Here set Fade ou immediateley (idk how do)

        // Set Box Collider FALSE, no more extra points =]
        this.GetComponent<BoxCollider2D>().enabled = false;

        score.AddScore (point);

        // Destroy object after 1 sec, now can play efx sound
        Destroy (gameObject, 1f);
    }

    if (colisor.gameObject.tag == "floor") {
        Destroy (gameObject, 1.5f);

}

Current CODE:

    void OnCollisionEnter2D(Collision2D colisor)
{
    if (colisor.gameObject.tag == "Bee") {

        GetComponent<AudioSource> ().Play();
        score.AddScore (point);
        Destroy (gameObject);
    }

    if (colisor.gameObject.tag == "floor") {
        Destroy (gameObject, 1.5f);

}

Solution

  • You don't need to fade anything. Just attach the AudioSource to the Bee GameObject that does not get destroyed then get reference to it in the Start function. Also use gameObject.CompareTag instead if gameObject.tag

    AudioSource coinCollectSound;
    
    void Start()
    {
        coinCollectSound = GameObject.Find("Bee").GetComponent<AudioSource>();
    }
    
    void OnCollisionEnter2D(Collision2D colisor)
        {
            if (colisor.gameObject.CompareTag ("Bee"))
            {
    
                coinCollectSound.Play();
    
                score.AddScore (point);
                Destroy(gameObject);
            }
    
            if (colisor.gameObject.CompareTag("floor"))
            {
                Destroy(gameObject, 1.5f);
    
            }
    }
    

    EDIT:

    Not mentioned in your question that you have 3 sounds.

    Create GameObjects named COINSOUNDS then create 3 extra GameObjects under it. Rename them to COINSOUND1,COINSOUND2,COINSOUND3 and attach AudioSource to each of them. Don't attach AudioSource to the COINSOUNDS(parent GameOBject).

    COINSOUND1,COINSOUND2,COINSOUND3 must be child of COINSOUNDS GameObject.

    AudioSource[] coinCollectSound;
    
    void Start()
    {    
        coinCollectSound = new AudioSource[3];
        coinCollectSound[0] = GameObject.Find("COINSOUNDS/COINSOUND1").GetComponent<AudioSource>();
        coinCollectSound[1] = GameObject.Find("COINSOUNDS/COINSOUND2").GetComponent<AudioSource>();
        coinCollectSound[2] = GameObject.Find("COINSOUNDS/COINSOUND3").GetComponent<AudioSource>();
    }
    
    
    void OnCollisionEnter2D(Collision2D colisor)
    {
        if (colisor.gameObject.CompareTag("Bee"))
        {
    
            coinCollectSound[0].Play();//Play COINSOUND1
            coinCollectSound[1].Play();//Play COINSOUND2
            coinCollectSound[2].Play();//Play COINSOUND3
    
            score.AddScore (point);
            Destroy(gameObject);
        }
    
        if (colisor.gameObject.CompareTag("floor"))
        {
            Destroy(gameObject, 1.5f);
    
        }
    }
    

    SOLUTION 3

    You can also use coroutine. Start coroutine, fade the image, play sound, wait for sound to finish playing, then destroy. This can only be done in coroutine. This looks better than my other solution.You don't have to modify anything in your current scene with the code below.

    SpriteRenderer coinSpriteRenderer;
    AudioSource coinCollectSound;
    
    void Start()
    {
        coinCollectSound = gameObject.GetComponent<AudioSource>();
        coinSpriteRenderer = gameObject.GetComponent<SpriteRenderer>();
    }
    
    void OnCollisionEnter2D(Collision2D colisor)
    {
        if (colisor.gameObject.CompareTag("Bee"))
        {
    
            score.AddScore (point);
            StartCoroutine(fadeAndWaitForSound(1));
        }
    
        if (colisor.gameObject.CompareTag("floor"))
        {
            StartCoroutine(fadeAndWaitForSound(1));
        }
    
    }
    
    IEnumerator fadeAndWaitForSound(float fadeTimeInSeconds = 1)
    {
    
        Color currentColor = coinSpriteRenderer.color;
    
        Color invisibleColor = coinSpriteRenderer.color;
        invisibleColor.a = 0; //Set Alpha to 0
    
    
        float counter = 0;
    
        //Play sound
        coinCollectSound.Play();
    
        //Wait till sound is done playing
        while (coinCollectSound.isPlaying)
        {
            yield return null; 
        }
    
        //Now Fade texture
        while (counter < fadeTimeInSeconds)
        {
            counter += Time.deltaTime;
            coinSpriteRenderer.color = Color.Lerp(currentColor, invisibleColor, counter / fadeTimeInSeconds);
            yield return null;
        }
    
        //Destroy after fading
        Destroy(gameObject);
    }