- Home /
How to Detect Which Sprite a Sprite Renderer Is Using?
Hello, everyone. I have a stupid question: I want to set a boolean to true if an object's sprite renderer is using a certain sprite, however I am unsure how to do so. When I use this code I get an error which says "The name sprite does not exist in the current context":
bool myBool;
SpriteRenderer sr;
void Start () {
sr = GetComponent<SpriteRenderer>();
}
void Update () {
if (sr.sprite == mySprite)
{
myBool = true;
}
}
If anyone knows how to do this correctly, please tell me. Thanks.
Answer by xxmariofer · Jan 20, 2019 at 07:47 PM
Your code is fine, so the problem is mySprite never be assigned (not sure if its in inspector or where) or the spriterenderer of the object who has that monobehaviour doesnt have a sprite. i have just tested and works fine.
Your answer
Follow this Question
Related Questions
Swapping sprite of game object that has an animator. (Unity 2017.3.1) 0 Answers
Sprite Previews not displaying! 0 Answers
Gui Box - Texture - Spritesheet 0 Answers
Prefabs and multiple tilesets? 0 Answers
Problem with a Bool 1 Answer