[Problem] Code explanation

I think i understand this piece of code, and i try to explain it to my self out loud but i find little black holes in my explanation.
Can someone explain to me in a very clear way why are we doing this?:

> if (Time.timeSinceLevelLoad < fadeInTime) { -----------------------> this one in particular
>     float alphaChange = Time.deltaTime / fadeInTime;-------------> this one i think is the proportion of color that we take in every frame to the alpha, but not sure

thanks in advance

1 Like

It helps a lot if you include the entire script rather than just one snippet, as it provides the context for what’s going on. Even just providing the script name makes it easier to find on GitHub.

fadeInTime is the time over which the image will fade since the level loads. so if it’s 2.0, the fade will occur over 2 seconds.

Dividing the time since the last frame (Time.deltaTime) by the fadeInTime gives you the amount the alpha should change in that frame.

So if fadeInTime is 2.0, and the frame took 1s to render, the alpha should change by 0.5. So the script then subtracts the alphaChange from currentColor.a (Color.black starts with 1.0 alpha).

Make sense?

public class FadeIn : MonoBehaviour {

	public float fadeInTime;
	private Image fadePanel;
	private Color currentColor = Color.black;

	// Use this for initialization
	void Start () {
		fadePanel = GetComponent<Image>();
	// Update is called once per frame
	void Update () {
		if (Time.timeSinceLevelLoad < fadeInTime) {
			// Fade in
			float alphaChange = Time.deltaTime / fadeInTime;
			currentColor.a -= alphaChange;
			fadePanel.color = currentColor;
		} else {
			gameObject.SetActive (false);

Hello Miguel, asking the community to clarify something is a very good habit, please keep doing this since sometimes the question that you pop up can help other people understand better how programming works, and those little differences like this code that you brought is what really makes the difference between trully understanding how the programming logic works and just memorizing what a couple of methods do.

Well, time is an progressive value, which is obvious but it is a important thing to remember, since you can use it to make something run in specific intervals of time.

In this case he is using Time.timeSinceLevelLoad, a method that returns the time in seconds as a float since the start of the scene, and he compares it to the float fadeInTime which is a value meant to represents the point in time when the code inside the if statement iis supposed to run until.

The Update method is called once per frame, which means that the code inside the if will run every frame until the time has reached the fadeInTime. It is important to note that Update() is not called on a regular frequence, the time between Update() calls may vary.
Time.deltaTime is the time in seconds that took to complete the last frame, it is the time between each Update() call.
By using float alphaChange = Time.deltaTime / fadeInTime,, you are saying that alphaChange is a value that represents the portion of the fadeInTime referent to the amount of time that the code took to complete the last frame.

If you add everything up you will have a method that run at x interval of time and for each time that it runs, it will compute the proportion of the fadeInTime that the x value of time represents, and it will take it out of the alpha value (which ranges from 0 to 1). For example (it is just an example, the time between updates is way smaller than the following):

lets say that Update took 0.5 seconds to run(Time.deltaTime) the first time, and fadeintime is 3, so the alphaChange will be a fraction of 1/6 of the fadeintime, and you will take 1/6 out of the alpha channel in this run
the second time the Update takes 1 second to run (Time.deltaTime), fadeintime is still 3, so the alphaChange will be 1/3, now you take more 1/3 out of the alpha channel, which now is 3/6 (1/6+1/3).

Eventually, the update will run until it reaches the fadeintime, and the alpha channel will be changed accordingly untill it reaches 0. It is a way to make sure that the alpha channel will always be the same proportion of the time that you want it to fade into alpha 0, it will decreasse evenly.


@Joao_Dalvi @ninjachimp thank you both for the answer. I will try to be more clear next time in my questions.

Now i have a more clear vision mof what we are doing in this part of the code.



Privacy & Terms