

One of the common errors in Unity development is:
ArgumentNullException: Value cannot be null
This exception occurs when a method receives a null argument that it cannot handle. Unlike NullReferenceException, which happens when you access a null object, ArgumentNullException is triggered when a method explicitly requires a non-null value, and the value passed is null. This guide will explain why this happens, common scenarios, and how to fix it.
An ArgumentNullException is a .NET exception that occurs when a method receives a null argument but expects a valid object or value. Unity, being built on C# and .NET, throws this exception when null arguments are passed to functions that cannot accept them.
Example console output:
ArgumentNullException: Value cannot be null.
Parameter name: target
at UnityEngine.Transform.LookAt(Transform target)
at PlayerController.Update() ...
In this case, the LookAt method expects a valid Transform, but the target passed was null.
Unity methods like Instantiate(), Transform.LookAt(), or Physics.Raycast() require non-null arguments. Passing null will immediately throw an ArgumentNullException:
Transform target = null; transform.LookAt(target); // Throws exception
If a public or [SerializeField] variable is not assigned in the Inspector and you pass it to a method expecting a valid reference, the exception occurs:
public GameObject enemy;
void Start() {
SpawnEnemy(enemy); // enemy is null
}
If a method returns null and you pass its result to another method, it can trigger the exception:
GameObject FindEnemy() {
return null;
}
void Start() {
Destroy(FindEnemy()); // ArgumentNullException
}
Passing null parameters to delegates or events that expect valid objects can also cause this exception:
public delegate void OnTargetSelected(GameObject target); OnTargetSelected targetEvent; targetEvent?.Invoke(null); // Throws ArgumentNullException
Some Unity API methods do not accept null strings:
string sceneName = null; SceneManager.LoadScene(sceneName); // Throws ArgumentNullException
Verify that any variable or object you pass to a method is not null:
if(target != null) {
transform.LookAt(target);
} else {
Debug.LogWarning("Target is null");
}
Make sure all public or [SerializeField] fields are assigned to valid objects in the Unity Inspector. This is often the simplest fix.
If your code creates objects at runtime, initialize them before passing them to functions:
GameObject enemy = new GameObject("Enemy");
SpawnEnemy(enemy); // Safe
If an argument might be null, consider providing a default object or value:
if(target == null) target = defaultTarget; transform.LookAt(target);
In your own methods, check for null arguments and throw meaningful warnings instead of letting Unity crash:
void SpawnEnemy(GameObject enemy) {
if(enemy == null) {
Debug.LogError("Cannot spawn a null enemy!");
return;
}
Instantiate(enemy);
}
Unity will tell you exactly which method and line received a null argument. Start debugging at that line.
Debug.Log("Enemy: " + enemy);
Debug.Log("Target: " + target);
This helps identify which variable is null.
Setting breakpoints allows you to inspect all arguments before the method call. This is especially helpful for events or dynamically created objects.
ArgumentNullException is a common runtime error in Unity that occurs when a null argument is passed to a method that expects a valid object. By checking arguments, assigning variables in the Inspector, initializing objects, and using defensive programming, you can prevent this exception from occurring in your Unity projects.
Understanding and addressing ArgumentNullException will improve the stability and reliability of your code, making your development experience smoother and more predictable.