2 typo
source | link

You need to use curly braces { } to group together statements that you want to execute only when the condition of the if statement is met.

if (condition)
   DoActionA();

DoActionB();

This says "Do action A only if condition is true, but always do action B no matter what"

if (condition)
{
    DoActionA();
    DoActionB();
}

This instead says "Do action A then action B only if condition is true, otherwise skip them both"

So, specific fixes:

  1. Don't call GetComponent every frame. That's just wasteful. Instead, call it during start-up and cache the result.

  2. Put the sprite assignments inside braces to bundle them with the if statements that should control them.

Like so:

// You could just make these public variables and assign the sprite in the inspector.
Sprite UpSprite;upSprite;
Sprite LeftSprite;leftSprite;

SpriteRenderer myRenderer;

void Start()
{
    dest = transform.position;
    speed = 25;

    // Cache our sprites & renderer.
    myRenderer = GetComponent<SpriteRenderer>();
    upSprite = GameObject.Find("up").GetComponent<SpriteRenderer>().sprite;
    leftSprite = GameObject.Find("left").GetComponent<SpriteRenderer>().sprite;  
}

void FixedUpdate()
{
    if (Input.GetKey("w")) {
        transform.position = Vector3.MoveTowards(transform.position, points[0].position, speed * Time.deltaTime);
        myRenderer.sprite = upSprite;
    }
    if...

You need to use curly braces { } to group together statements that you want to execute only when the condition of the if statement is met.

if (condition)
   DoActionA();

DoActionB();

This says "Do action A only if condition is true, but always do action B no matter what"

if (condition)
{
    DoActionA();
    DoActionB();
}

This instead says "Do action A then action B only if condition is true, otherwise skip them both"

So, specific fixes:

  1. Don't call GetComponent every frame. That's just wasteful. Instead, call it during start-up and cache the result.

  2. Put the sprite assignments inside braces to bundle them with the if statements that should control them.

Like so:

// You could just make these public variables and assign the sprite in the inspector.
Sprite UpSprite;
Sprite LeftSprite;

SpriteRenderer myRenderer;

void Start()
{
    dest = transform.position;
    speed = 25;

    // Cache our sprites & renderer.
    myRenderer = GetComponent<SpriteRenderer>();
    upSprite = GameObject.Find("up").GetComponent<SpriteRenderer>().sprite;
    leftSprite = GameObject.Find("left").GetComponent<SpriteRenderer>().sprite;  
}

void FixedUpdate()
{
    if (Input.GetKey("w")) {
        transform.position = Vector3.MoveTowards(transform.position, points[0].position, speed * Time.deltaTime);
        myRenderer.sprite = upSprite;
    }
    if...

You need to use curly braces { } to group together statements that you want to execute only when the condition of the if statement is met.

if (condition)
   DoActionA();

DoActionB();

This says "Do action A only if condition is true, but always do action B no matter what"

if (condition)
{
    DoActionA();
    DoActionB();
}

This instead says "Do action A then action B only if condition is true, otherwise skip them both"

So, specific fixes:

  1. Don't call GetComponent every frame. That's just wasteful. Instead, call it during start-up and cache the result.

  2. Put the sprite assignments inside braces to bundle them with the if statements that should control them.

Like so:

// You could just make these public variables and assign the sprite in the inspector.
Sprite upSprite;
Sprite leftSprite;

SpriteRenderer myRenderer;

void Start()
{
    dest = transform.position;
    speed = 25;

    // Cache our sprites & renderer.
    myRenderer = GetComponent<SpriteRenderer>();
    upSprite = GameObject.Find("up").GetComponent<SpriteRenderer>().sprite;
    leftSprite = GameObject.Find("left").GetComponent<SpriteRenderer>().sprite;  
}

void FixedUpdate()
{
    if (Input.GetKey("w")) {
        transform.position = Vector3.MoveTowards(transform.position, points[0].position, speed * Time.deltaTime);
        myRenderer.sprite = upSprite;
    }
    if...
1
source | link

You need to use curly braces { } to group together statements that you want to execute only when the condition of the if statement is met.

if (condition)
   DoActionA();

DoActionB();

This says "Do action A only if condition is true, but always do action B no matter what"

if (condition)
{
    DoActionA();
    DoActionB();
}

This instead says "Do action A then action B only if condition is true, otherwise skip them both"

So, specific fixes:

  1. Don't call GetComponent every frame. That's just wasteful. Instead, call it during start-up and cache the result.

  2. Put the sprite assignments inside braces to bundle them with the if statements that should control them.

Like so:

// You could just make these public variables and assign the sprite in the inspector.
Sprite UpSprite;
Sprite LeftSprite;

SpriteRenderer myRenderer;

void Start()
{
    dest = transform.position;
    speed = 25;

    // Cache our sprites & renderer.
    myRenderer = GetComponent<SpriteRenderer>();
    upSprite = GameObject.Find("up").GetComponent<SpriteRenderer>().sprite;
    leftSprite = GameObject.Find("left").GetComponent<SpriteRenderer>().sprite;  
}

void FixedUpdate()
{
    if (Input.GetKey("w")) {
        transform.position = Vector3.MoveTowards(transform.position, points[0].position, speed * Time.deltaTime);
        myRenderer.sprite = upSprite;
    }
    if...