1

I have a canvas for UI, with an image the same size of the canvas. The Image has an rgba of 0,0,0,0 , making it invisible (because the a is 0). I want to have the image fade into black from a script. Here is the code I am using:

public class NavigationC : MonoBehaviour {
    public Image screen;
    float fadeTime = 3f;
    Color colorToFadeTo;

    void StartGame()
    {
        colorToFadeTo = new Color(0f, 0f, 0f, 255f);
        screen.CrossFadeColor(colorToFadeTo, fadeTime, true, true);
    }
}

Now, this doesn't do anything when executed. But when I change the image's a value manually in unity so that the image becomes visible, I can see the script changing the image's color. So the script does work, it just isn't visible because the a value is not being changed by the script. So how can I make the image fade from invisible to black?

Programmer
  • 121,791
  • 22
  • 236
  • 328
RnRoger
  • 682
  • 1
  • 9
  • 33

1 Answers1

6

Here is your problem:

new Color(0f, 0f, 0f, **255f**);

The Color constructor parameter takes values from 0f to 1f but you are passing 0f to 255f range value to it.

That should be:

colorToFadeTo = new Color(0f, 0f, 0f, 1f);

If you want to use the 0 to 255 range then you must divide it by 255.

colorToFadeTo = new Color(0f, 0f, 0f, 255f/255f);

Also, there is Color32 which can take values between 0 and 255. You can use that then covert it back to color.

Color32 color32 = new Color32(0f, 0f, 0f, 255f));
Color color = color32;
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Then why does 255f work **perfectly** when I give the image a color to start with? – RnRoger Feb 19 '17 at 12:05
  • And I tried 1f before going to 255f, 1f didn't work either. – RnRoger Feb 19 '17 at 12:06
  • And now I can't even get 255f or any other value to work, even when I give the picture a color to start with; it just jumps to black or doesn't change at all. – RnRoger Feb 19 '17 at 12:34
  • Read the [doc](https://docs.unity3d.com/ScriptReference/Color.html) if you are still in doubt. It clearly says that it expects range from `0f` to `1f`. Look at all the static variables in that link. They use `0` to `1`. None of them uses `255` or anything above `1`. If it is changing so fast, pass `false` to the `ignoreTimeScale` parameter. `screen.CrossFadeColor(colorToFadeTo, fadeTime, false, true);` – Programmer Feb 19 '17 at 15:26
  • Like I said... I did try `1f`. Just saying that unity doesn't behave differently when I alternate between `1f` and `255f`. They both work or don't work, though obviously not how I want them to. In the end I fixed the issue by putting the image's `a` to 1 out of 255. I don't know why that works, but starting with 0 doesn't, but it works nonetheless. I don't doubt your answer and I know what you say is the full truth, just saying that it is not the one thing causing this issue. – RnRoger Feb 19 '17 at 16:03
  • My image `a` default is set to `1` (out of 255) instead of `0`, and I use `255f` instead of `1f`. **I know** that this is not supposed to work, and I know that you are right about everything you said, yet if I change EITHER of these values, it breaks. – RnRoger Feb 19 '17 at 16:06
  • It's just that your first three comment is talking about 255. As for your last comment, it may be working for you now but this is an *undefined* behavior. It may not work on other platforms. You may get another behavior anytime after updating Unity. I feel like this question is totally missing a lot of information. Can you create a new question and include the default values which you never mentioned in your question. Show animated image of what is currently happening. I will take a look at it. Just make sure you don't include 255 in it. That's up to you. I don't think this problem is solved. – Programmer Feb 19 '17 at 16:16
  • I will make a new question with video soon because I agree that what I did is just a workaround, but I did state the values... "The image has an rgba of 0,0,0,0 , making it invisible (because the a is 0)." – RnRoger Feb 19 '17 at 16:59
  • Forgot to mention, I did change `CrossFadeColor` to `CrossFadeAlpha`, it works but with the same issues... – RnRoger Feb 19 '17 at 17:04
  • Ok, I will wait for your new question. Just want to see the current behavior. – Programmer Feb 19 '17 at 17:07