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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Flickering Black Screen on Build but Editor Works Fine

Discussion in 'Scripting' started by _Prism_, Apr 27, 2022.

  1. _Prism_

    _Prism_

    Joined:
    Jan 25, 2014
    Posts:
    11
    Hi all,

    So the title pretty much sums up the problem I'm seeing.
    When I run my game in the editor, everything's fine, but when I build and run on the same system, I get a black flickery screen.

    Here are some details and things I've tried:

    1) The game is a top-down shooter, one of the weapons is a rifle that has a 'scope' effect which simply circle-wipes to simulate zooming, shrinking the visible area as the reticule moves further from the player.

    2) I've run a build on a Windows laptop and it works fine, but a build running on my iMac encounters the flickering issue. Another user has successfully run the game on a Windows desktop.

    3) I've isolated the issue to my calling of the OnRenderImage function to handle the above 'zooming' effect. Even if I remove the code I use inside that function call, as long as the function is being called I still experience the issue. Commenting out the OnRenderImage call all together, the game runs fine on my system, just without the zooming effect.

    4) I've established that the game is still running, even with the black screen issue, as I can hear the sound effects when a gun is fired.

    Any suggestions or ideas would be appreciated =)
    If anyone would like to test this out and tell me if they see the same issue, that'd be awesome too.
    Here's a link:

    https://bitglassgames.itch.io/controlled-chaos

    Looking around the forum, I can't see an issue that relates directly to what I'm seeing.
    Having said that I am completely new to shaders so if there is a solution out there and I'm missing it, or if there's a better way to achieve the effect I want, I'd be really happy for any assistance =)
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    It sounds like z fighting might be a possible source of issue, but it's hard to tell without seeing some code. Use code tags and share your code that you think is the source of the issue so people can maybe look. If you have a video of the issue, that helps also. It's rare that people want to download and run your program. Maybe someone will, but it's not as common.
     
    _Prism_ likes this.
  3. _Prism_

    _Prism_

    Joined:
    Jan 25, 2014
    Posts:
    11
    To try and solve the issue/learn more about shaders I jumped into this tutorial by Matt Schell ( https://learn.unity.com/tutorial/writing-your-first-shader-in-unity# ).
    He mentioned the ZWrite parameter and I remembered a post on the forum ( https://forum.unity.com/threads/black-screen-after-load.310798/#post-2052191 ) where issue was related to Z filtering.
    I figured since the game was running and this black screen simply appeared to be blocking the rest of the visual data, I added ZTest Always and shazzam! it works!

    For anyone wondering, here's shader code:

    Code (CSharp):
    1. Shader "Unlit/CircleWipeShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Radius ("WipeRadius", float) = 0
    7.         _Ratio ("ScreenRatio", float) = 0
    8.     }
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 100
    13.  
    14.         ZTest Always
    15.  
    16.         Pass
    17.         {
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             // make fog work
    22.             #pragma multi_compile_fog
    23.  
    24.             #include "UnityCG.cginc"
    25.  
    26.             struct appdata
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.             };
    31.  
    32.             struct v2f
    33.             {
    34.                 float2 uv : TEXCOORD0;
    35.                 UNITY_FOG_COORDS(1)
    36.                 float4 vertex : SV_POSITION;
    37.             };
    38.  
    39.             sampler2D _MainTex;
    40.             float4 _MainTex_ST;
    41.             float _Radius;
    42.             float _Ratio;
    43.  
    44.             v2f vert (appdata v)
    45.             {
    46.                 v2f o;
    47.                 o.vertex = UnityObjectToClipPos(v.vertex);
    48.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    49.                 UNITY_TRANSFER_FOG(o,o.vertex);
    50.                 return o;
    51.             }
    52.  
    53.             fixed4 frag (v2f i) : SV_Target
    54.             {
    55.                 // sample the texture
    56.                 fixed4 col = tex2D(_MainTex, i.uv);
    57.                 // apply fog
    58.                 //UNITY_APPLY_FOG(i.fogCoord, col);
    59.                 //return col;
    60.                 float3 pos = float3(i.uv.x - 0.5, (i.uv.y - 0.5) / _Ratio, 0);
    61.  
    62.                 return length(pos) > _Radius ? fixed4(0, 0, 0, 0) : col;
    63.             }
    64.             ENDCG
    65.         }
    66.     }
    67. }
    And here's the c# controlling script:

    Code (CSharp):
    1. public class CircleWipeController : MonoBehaviour
    2. {
    3.     public Shader shader;
    4.    
    5.     private Material material;
    6.  
    7.     public float reticuleDistance = 0f;
    8.  
    9.     private bool aiming;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         material = new Material(shader);
    15.         material.SetFloat("_Radius", 1000.0f);
    16.         material.SetFloat("_Ratio", 1.0f);
    17.     }
    18.  
    19.     private void OnRenderImage(RenderTexture source, RenderTexture destination)
    20.     {
    21.         Graphics.Blit(source, destination, material);
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         if(aiming)
    27.         {
    28.             float h = Screen.width * 1.0f;
    29.             float v = Screen.height * 1.0f;
    30.             float r = h / v;
    31.             material.SetFloat("_Radius", reticuleDistance);
    32.             material.SetFloat("_Ratio", r);
    33.         }
    34.     }
    35.  
    36.     public void SetRadius(float _retDist)
    37.     {
    38.         //set reticule distance
    39.         reticuleDistance = _retDist;
    40.         if(_retDist == 1000.0f)
    41.         {
    42.             material.SetFloat("_Radius", 1000.0f);
    43.             aiming = false;
    44.         }
    45.         else { aiming = true; }
    46.     }
    47. }
     
  4. _Prism_

    _Prism_

    Joined:
    Jan 25, 2014
    Posts:
    11
    Hey thanks for the reply. That's exactly what it was. I must've been typing my response when you replied.
    Managed to get it all working by adding ZTest Always to the shader ;-)
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Nice, glad you got it figured out and posted a solution!