Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Fading objects one by one

Discussion in 'Scripting' started by Section-One, Mar 1, 2018.

  1. Section-One

    Section-One

    Joined:
    Sep 8, 2014
    Posts:
    81
    I am trying to fade-in an array of gameobjects one by one.
    So far I've only been successful making them all fade-in simultaneously and to cause Unity editor to get stuck using 'while' loops.

    This is my fadeIn function interface which is located in a static class I use for helper functions:

    public static IEnumerator FadeIn3D(this Transform t, float targetAlpha, float duration)

    And in my regular class script that I use to start the game I have in my Start():

    for (int i = 1; i <= amountOfBuildings; i++) {
    GameObject building = Instantiate(randomBuildingPrefeb, randomBuildingPosition, Quaternion.Euler(-90, 0, 0), parentObject);
    building.transform.FadeIn3D(1, 1);
    }


    I need to make each Instantiate statement wait until the previous object has finished fading-in.
    And I need to continue running my code when all objects finished fading-in.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    For starters, use code tags:
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    Code (csharp):
    1.  
    2. for (int i = 1; i <= amountOfBuildings; i++) {
    3.  
    4.    GameObject building = Instantiate(randomBuildingPrefeb, randomBuildingPosition, Quaternion.Euler(-90, 0, 0), parentObject);
    5.  
    6.    building.transform.FadeIn3D(1, 1);
    7.  
    8. }
    9.  
    10.  
    Next, what you need to do is wait the duration between each instantiate and fade in:
    Code (csharp):
    1.  
    2. for (int i = 1; i <= amountOfBuildings; i++) {
    3.  
    4.    GameObject building = Instantiate(randomBuildingPrefeb, randomBuildingPosition, Quaternion.Euler(-90, 0, 0), parentObject);
    5.  
    6.    building.transform.FadeIn3D(1, 1);
    7.  
    8.    yield return new WaitForSeconds(1f); //wait 1 second, the duration fade in takes
    9.  
    10. }
    11.  
    Note, this for loop needs to be happening in a coroutine itself.

    I don't know what your FadeIn3d method does either... but it appears to return an IEnumerator. Why is that? Does that mean it should be ran as a coroutine? Maybe you can yield that instead of the WaitForSeconds.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    The easiest thing is to set Start to be a coroutine, and then yield the fades. Fun fact; Start is automatically run as a coroutine if you declare it as an IEnumerator. So it'd be:

    Code (csharp):
    1. private IEnumerator Start() {
    2.     //whatever you used to do here
    3.     for (int i = 1; i <= amountOfBuildings; i++) {
    4.        GameObject building = Instantiate(randomBuildingPrefeb, randomBuildingPosition, Quaternion.Euler(-90, 0, 0), parentObject);
    5.        yield return StartCoroutine(building.transform.FadeIn3D(1, 1));
    6.     }
    7.     //Whatever you used to do
    8. }
    EDIT: Zing! Sniped!
    I think yielding the coroutine is better than yielding a WaitForSeconds that's as long as the coroutine, though.
     
    Section-One likes this.
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    you could do a simple wait.

    Code (CSharp):
    1. public static IEnumerator FadeIn3D(this Transform t, float targetAlpha, float duration, float waitTime)
    2. {
    3.     yield return new WaitForSeconds(waitTime);
    4.     //then do your code to fadeIn
    5. }
    6.  
    7.  
    8.  
    9. float delayTime= 0;
    10. for (int i = 1; i <= amountOfBuildings; i++) {
    11.  
    12.    GameObject building = Instantiate(randomBuildingPrefeb, randomBuildingPosition, Quaternion.Euler(-90, 0, 0), parentObject);
    13.    building.transform.FadeIn3D(1, 1, delayTime);
    14.    delayTime += 1;
    15. }
     
  5. Section-One

    Section-One

    Joined:
    Sep 8, 2014
    Posts:
    81
    Thank you all for the answers!

    I've chosen @Baste's solution because it doesn't involve mimicking the fade-in delay, and it's more self-contained.
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    Yeah, it is better to do that.

    I just didn't know what that IEnumerator was, and your example code didn't call it as a coroutine. (IEnumerator isn't inherently a coroutine)
     
    Section-One likes this.