Answer
Let me start off by answering your question directly:
it works, can I ignore the error?
You could. You should not, because it means something is going wrong. You would get used to this error, but it could "hide" or cause another error.
Currently you have an error message and it still works correctly. The other way around, it not working and not having (or rather: not recognising) feedback why, is far worse!
Advice
To find out where this comes from, split this whole thing up into several lines.
string resourceLocation = "Prefabs/Items/" + someName;
Object prefab = Resources.Load(resourceLocation);
Object instance = Instantiate(prefab);
GameObject someObject = instance as GameObject;
An error only tells you at which line it happened. If the error happens in this code, the line number will tell you more about which part went wrong here. Also, I'd advise using the generic version of Resources.Load
, that would actually give us one step less to worry about:
string resourceLocation = "Prefabs/Items/" + someName;
GameObject prefab = Resources.Load<GameObject>(resourceLocation);
GameObject someObject = Instantiate(prefab);
Finding out why
- Now, a bit of Unity experience tells us that “The Object you want to Instantiate is null” is caused by
Instantiate()
.
- So, that means
prefab
is null
.
- So that means
Resources.Load
returns null
.
- The documentation for
Resources.Load
says "Returns the asset at path
if it can be found otherwise returns null."
- So that means it doesn't find the given path (the string I called
resourceLocation
)
Something is wrong with this path, so the obvious first step would be to see what it actually ends up being, with Debug.Log. As "everything works as intended", its probable that there is some duplication going on where one version works and the other gives you this error.
In that case, it's a good idea to use the 2 parameter version of Debug.Log Debug.Log(resourceLocation, gameObject);
. Now if you click on the log message in the Unity editor, it will select the GameObject
where it came from.