2D movement acting strangely

Here is the script in question:

using UnityEngine;
using System.Collections;

public class MissileAI : MonoBehaviour {
    private float PLAypos;
    private float PLAxpos;
    private float MYypos;
    private float MYxpos;
    private float spd;
    private Rigidbody2D rb;

    void Start () {
        spd = 0.05f; // Change this value to differ depending on the current level
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        PLAypos = GameObject.Find("Player").GetComponent<PlayerShoot>().playerYpos;
        PLAxpos = GameObject.Find("Player").GetComponent<PlayerShoot>().playerXpos;
        MYypos = transform.position.y;
        MYxpos = transform.position.x;

        if (PLAypos > MYypos && PLAxpos < MYxpos)
        {
            transform.position = new Vector2(MYxpos, MYypos + spd);
            transform.position = new Vector2(MYxpos - spd, MYypos);
        }

        else if (PLAypos < MYypos && PLAxpos < MYxpos)
        {
            transform.position = new Vector2(MYxpos, MYypos - spd);
            transform.position = new Vector2(MYxpos - spd, MYypos);
        }

        else if (PLAypos < MYypos && PLAxpos > MYxpos)
        {
            transform.position = new Vector2(MYxpos, MYypos - spd);
            transform.position = new Vector2(MYxpos + spd, MYypos);
        }
 
        else if (PLAypos > MYypos && PLAxpos > MYxpos)
        {
            transform.position = new Vector2(MYxpos, MYypos + spd);
            transform.position = new Vector2(MYxpos + spd, MYypos);
        }
    }
}

The missile only moves left and right, ignoring what i think is a correct script.
Any obvious fixes?

PLAypos gets the players y position, PLAxpos gets the players X position.

Thanks, its probably something obvious but i am quite new to unity.

Let’s look at just one of these, since they all have the same error:

         if (PLAypos > MYypos && PLAxpos < MYxpos)
         {
             transform.position = new Vector2(MYxpos, MYypos + spd);
             transform.position = new Vector2(MYxpos - spd, MYypos);
         }

What you’re doing here is: first, you’re setting the object’s position to the first Vector2. Then, you’re setting the object’s position to the second Vector2. Each time you do this, you’re not adding to the position, you’re replacing it, so the first Vector2 is essentially ignored.

So what you want to do is to do this:

         if (PLAypos > MYypos && PLAxpos < MYxpos)
         {
             transform.position = new Vector2(MYxpos - spd, MYypos + spd);
         }

Unrelated side note: It’s generally bad practice to use GameObject.Find() in the Update event, because it’s relatively time-consuming and can slow your game down if you leave it there and forget about it when your game gets bigger. If you have to use it, putting GameObject.Find() in the Start event and getting a permanent reference to the player is more efficient.

durr, thanks dude.