Quick answer: A method was handed null for an argument it requires. The second line of the message, Parameter name: X, names that argument in the method at the top of the stack trace: source is a collection passed to LINQ, key a dictionary key, s a string given to a parser, path a file path, shader the result of Shader.Find. Find the first frame in Assets/, look at the value you passed in that position, and fix where it should have been assigned.
ArgumentNullException: Value cannot be null looks like a vaguer cousin of NullReferenceException, but it carries more information. The method that threw it checked its arguments and told you which one was null. This guide lists the parameter names that turn up in Unity projects and what each one points to, shows how to walk from the stack trace to the null value, and gives the fix for each group, including the two that only appear in builds.
- The exact error
- How it differs from NullReferenceException
- What each parameter name means
- How to find the null argument
- Fix 1: collections and keys (source, collection, key)
- Fix 2: strings and paths (s, value, path, path1)
- Fix 3: Type and Shader lookups that return null in a build
- The _unity_self case
- How to confirm the fix
- Reading these in reports from players
- Related guides
- Frequently asked questions
The exact error
ArgumentNullException: Value cannot be null.
Parameter name: source
System.Linq.Enumerable.Where[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate) (at <...>:0)
QuestLog.GetActive () (at Assets/Scripts/QuestLog.cs:18)
Unity’s class libraries print the parameter on its own line as Parameter name: X. Where this page shows <...>, Unity prints a hex id that can be ignored. If you see the same error written on one line as Value cannot be null. (Parameter 'source'), the code is running on modern .NET outside Unity, for example in a separate server project. It means the same thing.
A few methods throw without naming a parameter, and then the message is only the first line. In that case the top stack frame is your only clue to which call refused its input.
How it differs from NullReferenceException
| NullReferenceException | ArgumentNullException | |
|---|---|---|
| What happened | You used a member of something that is null: enemy.health | You passed null into a method that checks its input: File.ReadAllText(path) |
| Who throws | The runtime, on your line | The method you called. The top frame is library or engine code. |
| What the message tells you | Nothing about which variable | The name of the parameter that was null |
| Where to look | The line in your script, left of each dot | The first frame in Assets/, at the argument that matches the parameter name |
One case confuses people. quests.Where(q => q.active) looks like a member call on quests, so a null quests seems as if it should give a NullReferenceException. LINQ methods are extension methods, and the compiler turns that line into Enumerable.Where(quests, ...). The list is an argument, so you get ArgumentNullException with Parameter name: source. For the other exception, see How to Fix Unity’s NullReferenceException.
What each parameter name means
The name is the parameter name in the signature of the method in the top stack frame. These are the ones you are likely to meet:
| Parameter name | Top frame is usually | What was null |
|---|---|---|
source | System.Linq.Enumerable (Where, Select, ToList, Count, Any, First) | The collection in front of the dot |
source | UnityEngine.AudioSource (Play, PlayOneShot) | The AudioSource the call was made on is missing or has been destroyed |
key | System.Collections.Generic.Dictionary`2 | The key given to the indexer, TryGetValue, ContainsKey or Add |
collection | List`1..ctor, List`1.AddRange | The collection passed to new List<T>(x) or AddRange(x) |
s | System.Int32.Parse, System.Single.Parse, System.Convert.FromBase64String | The string to parse or decode |
value | System.String (Contains, StartsWith) | The string you are searching for |
path | System.IO.File, System.IO.Directory, System.IO.FileStream..ctor | The file or folder path |
path1, path2 | System.IO.Path.Combine | The first or second part of the path |
type | System.Activator.CreateInstance, UnityEngine.JsonUtility.FromJson | A Type, usually because Type.GetType returned null |
shader | UnityEngine.Material..ctor | The shader, usually because Shader.Find returned null |
objectToOverwrite | UnityEngine.JsonUtility.FromJsonOverwrite | The object the JSON should be written into |
_unity_self | UnityEditor.SerializedObject and other engine methods | The Unity object the method was called on no longer exists. Mostly an editor error. |
When the top frame is a UnityEngine method and the parameter is a Unity object (a collider, a clip, a material), the usual meaning is that the object you passed was never assigned or has been destroyed. Passing a null prefab to Instantiate is the best-known case, and it has its own wording, covered in How to Fix “ArgumentException: The Object you want to instantiate is null” in Unity.
How to find the null argument
- Read the parameter name on the second line of the message.
- Read the top stack frame. It is the method that refused the value, and its signature is printed with parameter names. In the example,
sourceis the first parameter ofEnumerable.Where. - Go down to the first frame in
Assets/and double-click the Console entry. That line contains the call. - Match the name to what you passed. For an extension method, the first parameter is the value in front of the dot. Hovering the method name in your code editor shows the signature if the trace is not enough.
- Log it on the line above to confirm, then find where it should have been set:
Debug.Log($"quests is null: {quests == null}", this);
If one line chains several calls, such as GetQuests().Where(...).Select(...), the top frame says which link failed. A null return from GetQuests() fails in Where. A null inside the lambda fails with a NullReferenceException in your own code instead.
Fix 1: collections and keys (source, collection, key)
A collection is null because nothing created it. The common origins in Unity are a private list field with no initializer, a list on a plain class created with new, and one of your own methods that returns null to mean “nothing found”.
// Broken: never created
private List<Quest> quests;
IEnumerable<Quest> GetActive() => quests.Where(q => q.active);
// Fixed: create it where it is declared
private readonly List<Quest> quests = new List<Quest>();
Return an empty collection from your own methods, never null, so callers can chain LINQ without a check:
public IReadOnlyList<Quest> FindByZone(string zone)
{
if (!questsByZone.TryGetValue(zone, out List<Quest> found))
return System.Array.Empty<Quest>();
return found;
}
For data that arrives from a JSON library or another system, where a missing array can come through as null, normalize once at the boundary with data.items ?? new List<Item>().
A null key is nearly always a string id that was never assigned. Check it before the lookup, and say which object had the empty id:
if (string.IsNullOrEmpty(itemId))
{
Debug.LogError("Shop: item has no id assigned", this);
return;
}
if (!prices.TryGetValue(itemId, out int price)) { /* handle a missing item */ }
TryGetValue protects you from a missing key. It still throws on a null one.
Fix 2: strings and paths (s, value, path, path1)
int.Parse(null) and float.Parse(null) throw this exception with Parameter name: s. An empty or non-numeric string throws FormatException instead, which is covered in How to Fix “FormatException: Input string was not in a correct format” in Unity. TryParse handles all three without throwing:
// Broken: text is null when the config entry is missing
int lives = int.Parse(text);
// Fixed: null, empty and non-numeric input all return false
if (!int.TryParse(text, out int lives))
lives = 3;
For file paths, the null usually comes from a variable that is filled in later than it is used, or from a helper that returns null. Path.GetDirectoryName returns null for a root path, and passing that result on gives Parameter name: path in the next call. Build paths in one place, from values that are always available:
public static class SavePaths
{
public static string Slot(int slot)
{
return Path.Combine(Application.persistentDataPath, "Saves", $"slot{slot}.json");
}
}
A static method like this is safe to call from Awake onward. Do not move the call into a field initializer on a MonoBehaviour, because Unity does not allow persistentDataPath to be read there. If your next error is a permissions one, see How to Fix “UnauthorizedAccessException: Access to the path is denied” in Unity.
Fix 3: Type and Shader lookups that return null in a build
Two parameter names tend to appear only after you make a build, because both come from finding something by its name.
type
// Broken: type is null, so CreateInstance throws
Type type = Type.GetType("FireballAbility");
object ability = Activator.CreateInstance(type);
Type.GetType returns null without complaint when it cannot find the name. It needs the namespace ("Game.Abilities.FireballAbility"), and for a type in another assembly it needs the assembly name as well ("Game.Abilities.FireballAbility, Game.Abilities"). A plain name only finds types in the calling assembly and the core library, so code that worked stops working when the class moves into an assembly definition.
In a build there is a second reason. Managed code stripping removes classes that nothing references directly, and a class that is only ever named in a string looks unused. IL2CPP builds always strip. Keep the class with the [Preserve] attribute from UnityEngine.Scripting or a link.xml entry, as described in Fix: Unity IL2CPP Stripping Removes Needed Types. Better still, remove the string:
// Fixed: a real reference, checked by the compiler and visible to the stripper
private static readonly Dictionary<string, Func<Ability>> factories =
new Dictionary<string, Func<Ability>>
{
{ "fireball", () => new FireballAbility() },
{ "heal", () => new HealAbility() },
};
shader
// Broken in a build: Shader.Find returns null, the Material constructor throws
Material flash = new Material(Shader.Find("Custom/HitFlash"));
Shader.Find returns null when no shader has that name. In the editor every shader in the project is available. A build only includes shaders that something references, so a shader used only through Shader.Find is left out. There are three fixes, in order of preference:
- Reference a material or shader from a serialized field and stop looking it up by name:
[SerializeField] private Material flashMaterial;thennew Material(flashMaterial). - Add the shader to Edit > Project Settings > Graphics > Always Included Shaders.
- Put the shader, or a material that uses it, in a
Resourcesfolder.
The same missing shader on a material you did not create in code shows up as pink objects. That case is in How to Fix Pink (Magenta) Materials in a Unity Build.
The _unity_self case
ArgumentNullException: Value cannot be null.
Parameter name: _unity_self
UnityEditor.SerializedObject.FindProperty (System.String propertyPath) (at <...>:0)
_unity_self is the engine’s internal name for “the object this method was called on”. The C# object still exists, but the native Unity object behind it is gone. When every frame in the trace starts with UnityEditor or UnityEngine.UIElements, it is the Inspector holding on to an object that was destroyed or reloaded, and none of your game code is involved. Select a different object, or close and reopen the Inspector tab. If it keeps coming back, restart the editor and update to the latest patch release of your Unity version, since several reports of this message on Unity’s issue tracker were fixed in patch releases.
If the trace runs through your own editor script, you are using a SerializedObject or SerializedProperty after its target was destroyed. Create it again in OnEnable and do not keep it across selection changes. At runtime the message is rare. It has been reported for a UnityWebRequest that was disposed while a coroutine was still waiting on it, so dispose the request only after the coroutine has finished with it.
How to confirm the fix
Reproduce the null on purpose. Clear the list or the id field in the Inspector and enter Play mode. Delete the config entry the parser reads. Then make a build with the scripting backend and stripping level you ship with, and exercise every feature that creates an object from a type name or a shader name, since those two failures cannot be seen in the editor. The pass condition is no ArgumentNullException in the Console or in Player.log, and one of your own log lines for each value you removed.
Reading these in reports from players
The Bugnet Unity SDK files unhandled exceptions and error logs through Application.logMessageReceived, with the stack trace, recent log lines, game version, platform and OS. That makes it a good fit for the two build-only cases above, which you will not meet in the editor. One detail matters for this exception: a report’s title is the first line of the message, and the first line is always “ArgumentNullException: Value cannot be null.” The parameter name is on the second line, so it is in the report body with the stack trace, and every variant stacks into the same entry. Open the entry and read the body before assuming it is one bug.
Your own guard messages do not have that problem. “Shop: item has no id assigned” is filed as a separate entry with its own occurrence count. Bugnet cannot tell you why the value was null. It shows the call that refused it and what the game logged beforehand. A dashboard setting controls whether errors raised in the editor are filed, which keeps Inspector noise such as _unity_self out of the list. Setup is in the getting started guide.
Related guides
- How to Fix Unity’s NullReferenceException
- How to Fix “ArgumentException: The Object you want to instantiate is null” in Unity
- How to Fix “ArgumentOutOfRangeException: Index was out of range” in Unity
- How to Fix “FormatException: Input string was not in a correct format” in Unity
- How to Fix Pink (Magenta) Materials in a Unity Build
Frequently asked questions
What does “ArgumentNullException: Value cannot be null” mean in Unity?
A method checked its arguments and found null where a value is required. The second line of the message, Parameter name, gives the name of that argument in the method at the top of the stack trace. The fix is to find where the value you passed should have been assigned.
What does “Parameter name: source” mean?
In most cases a LINQ method such as Where, Select, ToList or Any was called on a collection that is null. LINQ methods are extension methods, so the collection in front of the dot is passed as an argument named source. If the top stack frame is UnityEngine.AudioSource, the AudioSource you called is missing or destroyed.
What is the difference between ArgumentNullException and NullReferenceException?
NullReferenceException means your own line used a member of something that is null. ArgumentNullException means you passed null into a method that validates its input, so the top stack frame is library code and the message names the parameter. The second is usually faster to diagnose.
Why do I get “Parameter name: shader” only in a build?
Shader.Find returns null when the shader is not in the build, and the Material constructor then rejects the null. A build only includes shaders that something references. Use a serialized material or shader reference, or add the shader to Always Included Shaders in the Graphics settings.
What is “Parameter name: _unity_self”?
It is the engine’s internal name for the object a method was called on, and it means that Unity object no longer exists. When the stack trace is all UnityEditor frames it is an Inspector error, usually cleared by selecting another object or restarting the editor. In your own editor code it means a SerializedObject outlived its target.
Read the second line before anything else. The parameter name, matched against the method in the top stack frame, tells you which of your values was null.