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