I have in my project a button used for attacks that changes the sprite (and other info) after each attack. When I left my work earlier today it worked perfectly. Now when I get home and have changed literally nothing, it no longer works?
The only thing I can think of that is different is that now I’m on wifi, but I haven’t downloaded anything new to my project. In fact I haven’t really changed much in my project. Why would this function just suddenly stop working even though I haven’t changed anything about it?
public Sprite image;
void Start()
{
image = attack.image;
}
I’ve checked in a million places and tried a bunch of debug codes. There’s literally no reason for this to stop working
void SetLeftAttack()
{
attack = gameController.attacksLeft[currentAttack];
image = attack.image;
Debug.Log(image);
Debug.Log(attack);
Debug.Log(attack.image);
}
so here is the full function that will change the sprite. Weird thing, the first debug and the last debug both say that the image is the same, the image that it should be (skill image) yet in game there is absolutely no change. WTF??? the icon doesn’t change at all, yet the code says its working perfectly??? Clearly it isn’t working at all. Did Unity change something that I am unaware of?
This code doesn’t do what you think it does.
You have a Sprite reference here, which does not display anything. It’s a reference to a single sprite asset in your project files. So you’re changing the sprite that your variable is holding, and that’s all.
If you want to display a sprite, you need to target a SpriteRenderer component. Or for UI, an Image component.
public SpriteRenderer image;
void Start()
{
image.sprite = attack.image;
}
That did it, for some reason. In my case I needed the Image component, which you mentioned, rather than SpriteRenderer.
The weird part is that I literally changed no code before the bug. Maybe it got imported wrong or something, or rebuilt? I dunno, but it is working now so I’m not going to dwell on it too long!
Thanks for the help!
1 Like