- Home /
Resources.Load sprite returning null (4.3)
Evening all,
I am attempting to programmatically populate my scene based on data in an XML file. I have parsed and created an object model from the XML no problem.
Now I am trying to create and add sprites to the scene. The sprite already exists within my assets folder.
I am doing the following where item.name matches the name of my sprite in unity.
var gameObject = (GameObject)Resources.Load(item.Name) as GameObject;
if(gameObject == null) Debug.LogError("item GameObject is null");
Instantiate(gameObject, new Vector3((float)item.UnityPositionX, (float)item.UnityPositionY, layer.Depth), new Quaternion(0,0,0,0));
However gameObject is always null.
Any suggestions or pointers would be much appreciated.
Thanks
Edit -
Through trial and error I got the following code to programmatic ally put a new 2d sprite type into the scene at runtime.
var gameObject = new GameObject ();
gameObject.transform.position = new Vector3 (0, 0, 0);
gameObject.AddComponent ("SpriteRenderer");
var renderer = (SpriteRenderer)gameObject.GetComponent ("SpriteRenderer");
renderer.sprite = Resources.Load<Sprite>("Grass50x50");
If you do a cast operation using parentheses, then converting with 'as' keyword is not required.
Path passed to
Resources.Load
must be relative to Resources folder, and file extension must be omitted. Does youritem.Name
follows these rules?
Yeah I'm really not convinced by your item.name maybe the name in the prenthesis it's not good ? It's exactly the same name as one of one of your prefab in Resources folder ?
ArkaneX, thanks yeah I understand with the casting. I am an experienced C# dev and that was taken from an example online. I removed it originally then added it back out of desperation.
Liszto, It is indeed exactly the same indeed.
Both,I will put together an isolated example and update the question with the resource name as a string etc. Wont be long.
A Sprite isn't a GameObject, if that's what you're trying to do. Also I'd recommend not putting sprites in Resources because they won't be able to generate atlases.
I have now tested in isolation and can get a sprite to load into gameobject so its obviously something else, thanks all. Scrub that it doesnt seem to be working. Think I need to get my head down and get a better feel for programmatic game object creation.
Eric - Even in 4.3? It was my understanding that it did? It would be great if you could expand further, would be a really appreciated.
Answer by mrlovepump · Nov 14, 2013 at 02:42 PM
OK, I have got the sprite object loading in correctly. I can only assume some of the examples out there are old and the objects dont work the way they once did.
The following loads a sprite from resources just fine.
var sprite = Resources.Load<Sprite>("Grass50x50");
Now the next stage is to use that to build up, in code a 2D sprite based game object in the same way it would be created using the Unity editor.
Please accept your answer or close the question if your problem is solved, thanks.
This works only for a single sprite that's the whole texture file; it doesn't work when you have a sprite sheet with a bunch of sprites defined. In that case you have to use LoadAll (with the path to the sprite sheet) to get all the sprites at once, and then inspect the resulting array to find what you need.
Thanks for this post! I am working with this issue and this really helps me a lot!
Neat! To assign the loaded sprite you can also use this ( for Image):
.GetComponent<Image>().overrideSprite = //Your sprite
I'm pretty sure that if you use .sprite ins$$anonymous$$d you won't get the job done
Answer by Melontxo88 · Jul 17, 2017 at 01:21 PM
public Image myImage;
myImage.sprite = (Sprite) Resources.Load("YourFolderName/YourSpriteName",typeof(Sprite));
For me works next two lines of code:
SpriteRenderer renderer = gameObject.GetComponent();
renderer.sprite = Resources.Load("YourFolderName/YourSpriteName");
Need to write full path to Resources folder. All files need which you need to use must be stored in resources folder:)
Your comment help me a lot
thanks
Answer by U_Ku_Shu · Aug 03, 2016 at 12:57 PM
Resources.Load are searching in the directory "Assets/Resources" That's why you need to do
_sprites = Resources.LoadAll<Sprite>(spritesPath);
or
_sprites = Resources.Load<Sprite>(spritesPath);
with spritesPath as relative path. If you need to load all from folder "Assets/Resources/Sprites", you need to write only "Sprites".
after this you can just do the following:
var sprite = sprites[0];
or
var sprite = _sprites.Where(a => a.name == "Sprite_Name_Needed").First();