- Home /
How to make screen turn black?
Hi.
I would like to make the screen turn black at an event (horror tricks).
I have tried doing it with the GUI.DrawTexture() function, but I can't seem to get it to work:
GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), blackTexture, ScaleMode.ScaleToFit, true);
"blackTexture" is a Texture variable, that I have assigned, but no luck. Always get the same error:
NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image, ScaleMode scaleMode, Boolean alphaBlend, Single imageAspect) UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image, ScaleMode scaleMode) MirrorScare+c__Iterator12.MoveNext () (at Assets/Scripts/MirrorScare.cs:20)
Use only GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), blackTexture); as you are already drawing it on the whole screen. If you still get a nullreference, than check if your variable is working. I normally use Texture2D ins$$anonymous$$d and never had any problem like this.
Please, check if your variable is working. Use 'print(blackTexture);' on 'Start()' and you will get null or the name of the texture. Besides this, there is no other element to be null on this line. Also, in your first code, you are missing one last value, the imageAspect.
Answer by Meltdown · Aug 10, 2012 at 07:47 PM
Have an inactive camera in your scene with the following settings...
Clear Flags - Solid Color
Background - Black
Culling Mask - Nothing
When you need the black screen, simply disable your main camera, and enable this camera.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075459im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
URP Template Project shows black screen 2 Answers
Black layout on "play" button 1 Answer
Black screen parts after "RUN" button is pressed 0 Answers
QCAR AR no working with gui menus 0 Answers
Black screen after success build. 1 Answer