- Home /
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 :D
Answer by nfkz · Jul 23, 2015 at 05:06 PM
I find the solution !
Resources.Load check only in Assets/Resources... Thks
Answer by allenallenallen · Jul 23, 2015 at 09:43 AM
Try changing this line:
newWall.GetComponent<SpriteRenderer>().sprite = Resources.Load("wall") as Sprite;
to this:
newWall.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("wall");
Answer by nfkz · Jul 23, 2015 at 05:52 PM
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.
Answer by nfkz · Jul 23, 2015 at 05:52 PM
I just find in other topic the solution !!!
Resources.Load check only in a Resources directory in Assets. (Assets/Resources). Thks for you reply !
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Best way of using sprites as text 0 Answers
Sprite flip movement script not working! HELP! 0 Answers
LocalScale not working idealy 1 Answer