- Home /
Null shader on camera at runtime?
I have a few custom post-effect esq shaders on my camera, all of which run great in editor but cause the following error to be thrown when built out and run. Each shader throws this same error in a similar location (material creation):
ArgumentNullException: Argument cannot be null.
Parameter name: shader
at (wrapper managed-to-native) UnityEngine.Material:CreateWithShader (UnityEngine.Material,UnityEngine.Shader)
at UnityEngine.Material..ctor (UnityEngine.Shader shader) [0x00000] in <filename unknown>:0
at DepthFog.Start () [0x0000c] in C:\Users\marsh\Dropbox\GameProjects\Cold\Cold\Assets\_Scripts\_Post\DepthFog.cs:20
(Filename: C:/Users/marsh/Dropbox/GameProjects/Cold/Cold/Assets/_Scripts/_Post/DepthFog.cs Line: 20)
The line it's complaining about in the script listed looks like this (the "mat = new Material(Shader..." bit):
void Start () {
mat = new Material(Shader.Find("Hidden/DepthFog"));
cam = gameObject.GetComponent<Camera>();
}
void OnRenderImage(RenderTexture src, RenderTexture dest){
if(mat != null){
cam.depthTextureMode = DepthTextureMode.Depth;
mat.SetColor("_Tint", FogTint);
mat.SetFloat("_Brightness", DepthBrightness);
mat.SetFloat("_DOffset", DepthOffset);
mat.SetFloat("_Squash", DepthSquash);
Graphics.Blit(src,dest,mat);
}
}
}
This screen effect and script worked fine in Unity 2017 (I recently upgraded), and (again) works great in editor. Hell, it doesen't even throw build errors, just run-time errors..
I've seen elsewhere that it might be linked to bad OS workspace mappings (a Unity bug mapping 32-bit asset paths as if they were 64), but I'm on a 64-bit OS, and the solutions to those would seemingly break my OS. Anyone have anything here? I'm at a loss.. Here's the full output log And here's the script (no compiler errors):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class DepthFog : MonoBehaviour {
[Range(0,10)]
public float DepthBrightness = 0f;
[Range(0,10)]
public float DepthSquash = 0f;
public float DepthOffset = 0f;
public Color FogTint = Color.white;
private Material mat;
private Camera cam;
// Use this for initialization
void Start () {
mat = new Material(Shader.Find("Hidden/DepthFog"));
cam = gameObject.GetComponent<Camera>();
}
void OnRenderImage(RenderTexture src, RenderTexture dest){
if(mat != null){
cam.depthTextureMode = DepthTextureMode.Depth;
mat.SetColor("_Tint", FogTint);
mat.SetFloat("_Brightness", DepthBrightness);
mat.SetFloat("_DOffset", DepthOffset);
mat.SetFloat("_Squash", DepthSquash);
Graphics.Blit(src,dest,mat);
}
}
}
Thanks for taking a look! I appreciate it a ton.
Answer by AdamSt · Nov 09, 2020 at 04:31 PM
For me the problem was a "Flare layer" added on camera. We were upgrading project from LWRP to URP, there was some post process on scene. After deleting object with PP this problem appeared. After deleting "Flare layer" from camera, problem disappeared.
Answer by vigadeveloper · Mar 01, 2019 at 04:03 AM
@Davidjsap I had a similar issue. I solved it by creating a shader variant collection and added the necessary shaders.
Your answer
![](https://koobas.hobune.stream/wayback/20220612181959im_/https://answers.unity.com/themes/thub/images/avi.jpg)