Calling Resources.Load on a texture is not working
Asked Answered
C

4

2

I'm trying to load a Texture from the Resources folder but it keeps on returning null.

t = (Texture)Resources.Load("Circle") as Texture;

The circle texture has an extension of .tga.

Cottier answered 18/4, 2013 at 20:12 Comment(8)
What programming language?Scarification
Added the C# tag for you.Scarification
Why cast to Texture and then try to parse the result to a Texture again?Theurgy
t = Resources.Load("Circle") as Texture;Cottier
I tried it this way too, I thought that maybe not casting it might've been the problem.Cottier
What about the path? Is it Assets/Resources/Circle.tga ?Faulty
Yes, that is the path.Cottier
public texture t = Resources.Load("Circle") as Texture; Try if that gives you some sort of error debug. Or that it still stays null . IF it stays null check capitalisation of your circle. Also check in editor if stays empty ofcourse ;)Sticktight
H
2

You must put Circle.tga in Assets/Resources folder. Plus if you have subfolder, for example Resources/Textures/Circle.tga then do like this:

Texture t = Resources.Load("Textures/Circle") as Texture;
Hydrokinetics answered 23/4, 2013 at 13:45 Comment(1)
"You must put Circle.tga in Assets/Resources folder." Thanks for this line.Eme
E
2

The extension of the file isn't necessary. So this is correct:

t = (Texture)Resources.Load("Circle") as Texture;

This is not:

t = (Texture)Resources.Load("Circle.npg") as Texture;
Exosphere answered 19/8, 2013 at 15:3 Comment(0)
P
2

Single Texture File

Texture File should be added into Resources folder After that use below lines of code for load texture

var Texture_1 : Texture2D;
Texture_1 = Resources.Load("TextureName");

For Folder

Folder should be in kept into Resources folder After that use below lines of code for load folder of textures

var textures : Object[];
textures = Resources.LoadAll("FolderName");
Philippi answered 2/9, 2013 at 11:17 Comment(1)
Thanks, my problem was the "Resources" folder, I did not know that.Renettarenew
K
1

@Massimiliano Uguccioni thanks... that fixed my problem.

Steps to use Resources.load:

  1. make sure its in the Resources folder
  2. reference full relative path without Resources: eg: textures/texture1 where textures is a folder inside Resources
  3. like Massimiliano Uguccioni said dont include file extension
Kovacs answered 30/9, 2021 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.