Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Help me, how to make this code work? fade out

Discussion in 'Scripting' started by Winkw, Sep 1, 2020.

  1. Winkw

    Winkw

    Joined:
    Apr 25, 2020
    Posts:
    4
    it is fade code,There are some mistakes,how to make this code work,I revised the question,I want to achieve the function, Game start,Hide this game object,OnTriggerEnter2D,to Fade in,after 3 seconds, automatically fadeout。


    Code (CSharp):
    1. public SpriteRenderer sprite;
    2. public float speed = 1f;
    3. public Color color = Color.white;
    4. public float duration = 3f;
    5.  
    6. void Start()
    7. {
    8.     sprite.color = new Color(1f, 1f, 1f, 0f);
    9. }
    10.  
    11. void OnTriggerEnter2D(Collider2D other)
    12. {
    13.     StartCoroutine(FadeIn());
    14. }
    15. void OnTriggerExit2D(Collider2D other)
    16. {
    17.     //StartCoroutine(FadeOut());
    18. }
    19.  
    20. IEnumerator FadeIn()
    21. {
    22.     sprite.color = new Color(1f, 1f, 1f, 0f);
    23.  
    24.     for (float alpha = 1.0f; alpha >= 0; alpha -= speed + speed * alpha)
    25.     {
    26.         sprite.color = new Color(1f, 1f, 1f, alpha);
    27.         yield return new WaitForSeconds(duration);
    28.     }
    29.  
    30.     sprite.color = new Color(1f, 1f, 1f, 0f);
    31. }
    32.  
    33. IEnumerator FadeOut()
    34. {
    35.  
    36.     sprite.color = new Color(1f, 1f, 1f, 1f);
    37.  
    38.     for (float alpha = 0; alpha <= 1.0f; alpha += speed + speed * alpha)
    39.     {
    40.         sprite.color = new Color(1f, 1f, 1f, alpha);
    41.         yield return new WaitForSeconds(duration);
    42.     }
    43. }
     
    Last edited: Sep 2, 2020
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Hey, writing "there are some mistakes, how to make this code work" in a programming forum is similar to just telling a mechanic to fix your car, but not telling him what's wrong with it in the first place. He just wont bother.

    This small article by Kurt-Dekker is a great place to look at for how to report problems: http://plbm.com/?p=220
     
    eses likes this.
  3. Winkw

    Winkw

    Joined:
    Apr 25, 2020
    Posts:
    4
    I'm an artist, I don't know how to modify it...
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Okay, but at the very least, state what "mistakes" there are with the script.
    What is it supposed to be doing and what is it actually doing?

    Edit:
    I can see one error at least.
    Your coroutine expects a SpriteRenderer and a float parameter, but you aren't providing any when you start it:
    Code (CSharp):
    1. StartCoroutune(fadeout());
    Did you maybe mean to use the "spriteToFade" and "duration" fields in your script?
    Code (CSharp):
    1. StartCoroutune(fadeout(spriteToFade, duration));
     
    Last edited: Sep 1, 2020
    gaglabs and Yoreki like this.
  5. gaglabs

    gaglabs

    Joined:
    Oct 17, 2019
    Posts:
    185
    Communication is good also. If you're the "artist" then there must be a "dev" that placed the code. Maybe communicate with them about the error. But as vryken said, it does in fact appear that the coroutine isnt being called correctly.
     
  6. Winkw

    Winkw

    Joined:
    Apr 25, 2020
    Posts:
    4
    Thank you. It looks like that,I want to achieve the function, Game start,Hide this game object,OnTriggerEnter2D,to Fade in,after 3 seconds, automatically fadeout,
     
  7. gaglabs

    gaglabs

    Joined:
    Oct 17, 2019
    Posts:
    185
    Ok... so what is the issue you are having? Does the sprite fade? Is there a collider attached? Is on trigger selected on the collider? Does the fade out get called? Did you run Debug to see if that function is called? There's alot you arent saying here.

    You are calling OnTriggerEnter. So that means you need a collider on the sprite with ontrigger selected. Otherwise you won't see anything happen. Secondly the code you placed above doesn't show the coroutine as what val suggested. And lastly, its good practice to run debug in your functions to see whats being called.
    Debug.Log("This is called");
    that way you know what works and what doesn't.
     
  8. gaglabs

    gaglabs

    Joined:
    Oct 17, 2019
    Posts:
    185
    I just seen you edited the above code. As far as I can see, your code should work properly. I think you sre missing the collider or on trigger.