Resources.Load returns null but Resources.LoadAll finds my file
Hello,
Right now I discover Unity and I have a little issue regarding Resource.Load
. The Texture I try to load is precisely Assets/Resources/Textures/Zero.png
and it's the only file under my Resources
folder.
If I execute below code:
m_Texture = Resources.Load<Texture2D>("Textures/Zero.png");
Debug.Log(m_Texture.name);
The debugger prints "Null". My resource is not loaded.
But if I use LoadAll
:
Object[] textures;
textures = Resources.LoadAll("Textures", typeof(Texture2D));
foreach (var t in textures)
{
Debug.Log(t.name);
}
Then I'll read "Zero" from the console.
More infos:
My script class declares following members:
private GameObject m_Object;
private Sprite m_Sprite;
private Texture2D m_Texture;
private SpriteRenderer m_RendererComponent;
Then in Start()
I load a Texture2D
and set it in a Sprite
// HERE I load the texture
m_Sprite = Sprite.Create(
m_Texture,
new Rect(0.0f, 0.0f, 100.0f, 15.0f),
new Vector2(0.5f, 0.5f)
);
m_Entity = new GameObject("Zero");
m_RendererComponent = m_Entity.AddComponent<SpriteRenderer>();
m_RendererComponent.sprite = m_Sprite;
Thank you for reading :)
Answer by marcobeck · Feb 06, 2021 at 07:00 AM
I know it's is a old topic, but can be usefull.
The error is to put the extension
You should call m_Texture = Resources.Load("Textures/Zero"); without the .png
Your answer
Follow this Question
Related Questions
how to assign video to albedo unity 0 Answers
Referring to sliced sprites in Resources by name 0 Answers
Resource.Load always returning null when trying to load an image 1 Answer
What is the best way to load Sprites dynamically? 0 Answers
Best way to make a skin system for a 2D character ? 0 Answers