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");

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.

public Image myImage;

myImage.sprite = (Sprite) Resources.Load(“YourFolderName/YourSpriteName”,typeof(Sprite));

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();