Resources.Load not working?!!

Hello!I have no ideea why this does not work :

var HM : Texture2D;

function Update(){

HM = Resources.Load(“Tex”,Texture2D);

}

–Tex is a jpeg image

Please help me,i am pulling my hair out! :shock:

What i wanna do is to assign the image Tex to var HM at runtime.

1 Like

the jpeg should be in the Resources folder for it to work.

2 Likes

you do not need the comma and Texture2D I don’t think. I think more likely, you aren’t storing your asset in a Resources folder. Make a folder called Resources and put it in there. It only works with that folder.

And um, please don’t put it in your Update loop or it will repeatedly try and load it forever, and probably freeze and die.

2 Likes

Thanks,it worked!

Hi,

I tried this and it is still not working for me. I have the textures in the assets folder and this is the code that I am using.

// c# code ///////////////////////////////////
Texture m_Down;
//Texture m_Up;
// Use this for initialization
void Start ()
{
//m_Up = Resources.Load(“Play”, typeof(Texture));
m_Down = (Texture)Resources.Load(“PressPlay”);
}

// Update is called once per frame
void Update ()
{
if(Input.GetMouseButtonUp(0))
{
if(this.guiTexture.HitTest(Input.mousePosition))
{
this.guiTexture.texture = m_Down;
}
}
}
///////////////////////////////////////////
Resources.Load always returns null.

resources.load will not load from the assets folder only from Assets/Resources
This folder is completely present in the build and thats a core requirement to load from code (for any other folder this is not the case, the stuff there is only included when assigned to game objects in scenes)

1 Like

Thank you. Now… that’s not clear in the documentation. In fact, it misleads us to believe that if we create some folders inside Resources we could use them to keep our stuff in a more organized way…

It’s not misleading. You can create subfolders under Resources. So, Assets/Resources/Player is perfectly valid, however if you want to target the stuff in there you have to do Resource.Load(“Player/filename”) should work(may need a slash before player also, but can’t recall that part off the top of my head). What you can’t do is create Assets/Player and then use Resource.Load to get the stuff out of that player folder.

2 Likes

Hello. I am new to Unity. I am trying to load a png file as a Sprite resource. I have read a hundred posts on Resources.Load failing and tried many things. Don’t know what is wrong. Here is my story.

I am testing this by pressing Play inside the Unity UI version ‘2018.1.1f1 personal’ I am running on OSX Sierra 10.13.5.

help1 is really help1.png stored in the Assets/Resources folder. When I moved it there Unity created a help1.png.meta file automatically.

35 Sprite s = Resources.Load(“help1”);
36 if (s != null)
37 Debug.Log("loaded help " + s.ToString());
38 else
39 Debug.Log(“resource load failed”);

when my code executes I get
resource load failed
UnityEngine.Debug:Log(Object)
HelpSwipeHandler:Start() (at Assets/Scripts/HelpSwipeHandler.cs:39)

The help1.png name is all lowercase. It is 1920x1080 in size, 72 pixels per inch, RGB color. It was created in Keynote and exported as a PNG image.

Any ideas? Any way to debug and get more info from what Resources.Load is doing?

Thanks.

Did you configure the “Texture Type” of “help1.png” to be a Sprite? Is it perhaps still using the default value instead?
https://docs.unity3d.com/Manual/class-TextureImporter.html

Thank you Peter. I had left the image as default. Works perfect now.

1 Like

Hello everyone.
I have the same problem with Resources.Load
1- I have the Resources folder.
2- the texture type of all assets in Resources folder is Sprite (2D and UI)

here is my code:

 private SpriteRenderer LaunchButtonSprite;

    void Start()
    {
        LaunchButtonSprite = gameObject.GetComponent<SpriteRenderer>();
        LaunchButtonSprite.sprite = Resources.Load("LaunchButtonWith7ShotSlot_3Full") as Sprite;
    }

The LaunchButtonSprite has another sprite before running. But when I run the game the sprite turns to “None”.

I found the solution.
Just changing the Resources.Load("LaunchButtonWith7ShotSlot_3Full") as Sprite to Resources.Load<Sprite>("LaunchButtonWith7ShotSlot_3Full") as Sprite

Please try:
Resources.Load<Sprite>("LaunchButtonWith7ShotSlot_3Full");

I guess if you don’t specify the type, it loads the main asset, which would be a Texture2D instead.

My issue was I included the extension “.png” in the path. It requires a path with no extension specified.

Resources.load only load from Resources folder, make a folder name Resources

9057160--1252006--ddd.PNG