Can't Flip the sprite with SpriteRenderer.flipX !

Hello, I’m trying to do a very simple code, but the code doesn’t work, don’t understand why!
I just to flip the the sprite when he reaches a certain position:

private SpriteRenderer mySpriteRenderer;

    private void Awake()
    {
        SpriteRenderer mySpriteRenderer = GetComponent<SpriteRenderer>();
    }

void Update () {
        if (transform.position.x < 0 && mySpriteRenderer != null)
        {
           mySpriteRenderer.flipX = true;
            Debug.Log("Flipped");
        }
       
    }

You created a local variable when you assigned your sprite renderer.
Change your awake method so it reads like so:

mySpriteRenderer = GetComponent<SpriteRenderer>();

Try that & see if it works :slight_smile:

You probably shouldn’t be needing to check that sprite renderer isn’t null in update. I mean, unless you plan on destroying that component… Just a thought for after you get it working :slight_smile:

2 Likes