What's The Difference Between "resources.load<type>("path")" Vs "resources.load("path") As Type"

Hello guys, I’ve been trying to use Resources.Load to load some sprite for my game. I noticed that when I load my sprite using “Resources.Load(“path”) as Sprite”, it doesn’t load properly. But if I use “Resources.Load(“path”)”, then it works fine. I’m curious for what’s happening underneath, if you know what’s up, please let me know!

“as” is used for subclasses. You can cast a subclass instance to a superclass, or vice versa. So this is valid:
SuperClass b = subclassInstance as SuperClass;

but this is not:
int i = subclassInstance as int;

is a template parameter. It can mean pretty much anything – two template substitutions can even refer to two completely different functions. For example, you could write code such that Load returned an object, while Load returned an integer.

I would guess that ‘Resources.Load(“path”) as Sprite’ doesn’t work, because Resources.Load(“path”) doesn’t return a Sprite or any of its superclasses. It probably doesn’t even look for Sprite data in the file. ‘Resources.Load(“path”)’, on the other hand, tells the code, “load the file, assuming it’s a Sprite”

1 Like