m_spriteButtonPressed=Resources.Load(“Sprites/Options/PatternButtonPressed.png”) as Sprite;
What are the reasons for this failing?
I definitely have a “Resources” folder under “Assets” in the unity editor.
And the path to the png is correct.
The name of the png file is correct.
Am I using the correct slash?
What else could go wrong?
Given that I have set the png as a 2D sprite in the editor, is “as Sprite” correct?
does it throw an error? in which case what’s the error? if not, how is it “failing”?
No exception error because I check for a null value in m_spriteButtonPressed before I use it.
But when stepping through this code with the debugger, after that function call, m_spriteButtonPressed is still null.
And my button remains invisible.
Don’t include the file extension.
Bloody hell!
It works this way: m_spriteButtonPressed=Resources.Load(“Sprites/Options/PatternButtonPressed”);
But not this way m_spriteButtonPressed=Resources.Load(“Sprites/Options/PatternButtonPressed”) as Sprite;
What the buggery is the difference???
I removed my file extensions but it still wouldn’t work.
My version of unity does not seem to like this “as Sprite”
well Load() knows it’s a sprite being returned when it’s run… the “as Sprite” version would need to do some sort of conversion from “object” to “sprite”. Although if the conversion was at fault I’d expect an error of some sort…
boylesg:
Bloody hell!
It works this way: m_spriteButtonPressed=Resources.Load(“Sprites/Options/PatternButtonPressed”);
But not this way m_spriteButtonPressed=Resources.Load(“Sprites/Options/PatternButtonPressed”) as Sprite;
What the buggery is the difference???
as Sprite will attempt to cast and return null if that cast fails. It may be finding something you’re not expecting since Sprite inherits from UnityEngine.Object so that cast should work if it’s actually finding a Sprite.