My GameObject (c#) haven't sprite defined

Hi, I try to create a GameObjet in script with a Component SpriteRenderer. When I play my scene, the new GameObject is create and I see it in the Hierarchy tree, but when I click on it, Sprite is “not assigned”.

My code :

		newWall = new GameObject("test");
		newWall.AddComponent<SpriteRenderer>();
		newWall.GetComponent<SpriteRenderer>().sprite = Resources.Load("wall") as Sprite;
		//or with inspector, i have a public var SpriteWall. I affect it in inspector of my script
		//newWall.GetComponent<SpriteRenderer>().sprite = SpriteWall;

		Vector3 pos = newWall.transform.position;
		pos.x = 0.2f;
		pos.y = 0.3f;
		pos.z = 5;
		newWall.transform.position = pos;
		newWall.SetActive(true);

When the game is playing, if I click on my gameobject and i see sprite not affected. I tried to select a sprite to test and it’s work, my gameobject appears.

An idea ? (Sorry for my english :/) Thks :smiley:

Try changing this line:

newWall.GetComponent<SpriteRenderer>().sprite = Resources.Load("wall") as Sprite;

to this:

newWall.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("wall");

Thks for you reply ! No changes with this line. But I just tried to get my Sprite in script (start function) :

		Sprite s = Resources.Load<Sprite>("wall");
		if (s == null)
			Debug.Log("Sprite is null !");
		else
			Debug.Log("Sprite is not null !");

But “Sprite is null”. My png is in my Assets directory. No subdirectory, directly in Assets.

I just find in other topic the solution !!!

Resources.Load check only in a Resources directory in Assets. (Assets/Resources). Thks for you reply !

I find the solution !

Resources.Load check only in Assets/Resources… Thks