- Home /
[C#]Adding a Sprite from a Resources.Load("Path") : How does it work ?
Hello uniters - not sure about this one ...
First thing first, here is the thing I got problem with :
if (SpriteSelector == null) { // if public Sprite variable is null
Object textureCheck = Resources.Load (defaultTexturePath);
if (textureCheck != null) {
SpriteSelector = textureCheck as Sprite; // here's the core problem
}
}
There's no more Log Error with the code, but SpriteSelector is still null after this.
I'm loading this within "void Start()", tried multiple thing I've found on the forum, but still can't add a default Sprite if none is selected.
The path within "defaultTexturePath" is valid, tried it with "Debug.Log()" by searching the number of files within the Sprite folder : Assets/Resources/2D/Textures. (the string for this variable is "2D/Textures/Diamond")
Diamond.png is within Textures folder and there is no fault within the string or the path.
The problem here is that I can't access the sprite with "Resources.Load()", I can load the Object class from it, but can't obtain its sprite ; Diamond.png asset as a Texture Type of Sprite within the editor when selected.
Does anyone as the solution for this problem ?
Answer by WhyldH · May 04, 2017 at 01:27 PM
"<" Sprite ">" was the answer... I'm feeling dumb... Ahahaha !
Just for the curious :
if (SpriteSelector == null) {
Sprite textureCheck = Resources.Load<Sprite> (defaultTexturePath);
if (textureCheck != null) {
SpriteSelector = textureCheck;
} }
@antx Thank you, dear Sir. ;)
Answer by antx · May 03, 2017 at 02:06 PM
private Sprite mySprite = null;
private string path = "2D/Textures/Diamond";
void Start()
{
mySprite = Resources.Load<Sprite>(path);
}
This should work.
"<"Sprite">" was the answer... I'm feeling dumb... Ahahaha !
Just for the curious : if (SpriteSelector == null) { Sprite textureCheck = Resources.Load"<"Sprite">" (defaultTexturePath); if (textureCheck != null) { SpriteSelector = textureCheck; } }
@antx Thank you, dear Sir. ;)
Answer by Icyteck01 · May 05, 2017 at 01:21 PM
i would load it async
IEnumerator loadSprite(string path)
{
UnityEngine.Object async = Resources.LoadAsync<Sprite>(path);
yield return async;
SpriteSelector = async as Sprite;
}
Your answer
Follow this Question
Related Questions
save & load sprite by path 0 Answers
Resources.Load sprite returning null (4.3) 3 Answers
How to assign Sprite in the inspector? 0 Answers
Loading Built-in resources 0 Answers
[2D] Object at X distance to Y direction from point 1 Answer