ArgumentException: The Object you want to instantiate is null
ArgumentException: The Object you want to instantiate is null.
Instantiate was given null instead of a prefab or object to copy. Older Unity versions word it as: The thing you want to instantiate is null.
Read the Console line first
The stack trace names the Instantiate call. Check the variable passed as its first argument.
Common causes
The prefab field is empty
The script's prefab slot shows None in the Inspector. Drag the prefab asset from the Project window into it.
Resources.Load returned null
The path is wrong, the asset is not inside a folder named Resources, or the path includes the file extension, which it must not.
The template was a scene object that got destroyed
Cloning an object from the scene works until that object is destroyed. Use a prefab asset as the template instead.
The fix
[SerializeField] private GameObject bulletPrefab;
void Fire()
{
if (bulletPrefab == null)
{
Debug.LogError("Assign Bullet Prefab in the Inspector", this);
return;
}
Instantiate(bulletPrefab, muzzle.position, muzzle.rotation);
}Stop it coming back
Reference prefabs through serialized fields instead of Resources.Load strings, so a missing one is visible in the Inspector.
Questions
- The prefab is assigned on the prefab asset. Why is it null in the scene?
- The scene instance can override the field. Select the object in the Hierarchy and check the slot there; a bold field means an override. Revert the override, or assign the prefab on the instance.
- Why does Resources.Load return null?
- The asset must sit inside a folder named
Resources, and the path you pass is relative to that folder, without the file extension:Resources.Load<GameObject>("Enemies/Goblin")forAssets/Resources/Enemies/Goblin.prefab. Check the spelling and case. - Is there a better way than Resources?
- For larger projects, Addressables load assets by address asynchronously and keep them out of the initial build. For a few prefabs, a serialized field on a component is simplest and fails visibly in the Inspector when empty.
Scripts that avoid it
Related errors
Got a different error? Paste it into the error lookup.