I want to move a Instantiated prefab from a specified point to another... Can you take a look at my logic?

I’m currently building demo of a game idea I have (“N01”). I was trying to write a script to;

  • Spawn a monster (which I made a prefab) on one of 3 spawn points where spawn point is randomly choosen, (which is Empty GameObject(s), I’m using that GO’s for their coordinates to specify where to spawn prefab);

  • Move that prefab to one of four DestinationPoints(which is GameObjects, again); based on position of Spawn Point/(s);

  • If prefab hits collider, which I set at edges of level/screen, stop moving and destroy that prefab clone

EDIT;

I have edited my code and moved code that moves the prefab to another script and put that in prefab as a component.
Right now instantiated object don’t move to specified DestinationPoints and Destroy, but they go back and forth (ie. one of clones going between; -7/0 on x, 0/2,5 on y etc.) very quickly and in Game Window it looks like they don’t move. To sum up, whatever I did, didn’t worked.
I used MoveTowards but it gave the result I explained above. I added a Rigidbody2D but it just made them disappear.
Tried Rigidbody2D.MovePosition(), and again, they disappeared instantly.

I’m going to share my code again, but before that let’s simplify the goal for now to find a good solution. After that I can modify it according to my requirements.


I want to Instantiate my monster prefab on a Spawn Point, which I use a empty GameObject as a reference, and move them to a Destination Point, which is again, a empty GameObject, where; I want a smooth transition, because that prefabs will come towards to Player, like in a FPS game state, and Player will jump over them to finish that level.
I set a collider on Levels edges (which is edges of the screen), and when the Clone/Prefab collides with it while going to the Destination, Prefab will be destroyed.
I hope someone will give me some idea about what am I doing wrong.

Here is the code I write;

MonsterSpawner, which is on a GO called “GameManager”;
using UnityEngine;

public class MonsterSpawner : MonoBehaviour
{
    public GameObject[] spawnPoints;
    
    public int monstersCount;

    public GameObject monster2;
    private GameObject monsterSprite;
    
    void Update()
    {
        SpawnMonster();
    }

    void SpawnMonster()
    {
        if (monstersCount < 1)
            return;

        int pointSelected = Random.Range(0, 3); //To use as index for reaching array of spawn points.
        Vector2 pointToSpawn = spawnPoints[pointSelected].transform.position;

        GameObject clonePrefab = Instantiate(monster2, pointToSpawn, Quaternion.identity);

        monsterSprite = clonePrefab.transform.GetChild(0).gameObject;
        if (pointSelected == 1)
            monsterSprite.GetComponent<SpriteRenderer>().flipX = true;
        else
            monsterSprite.GetComponent<SpriteRenderer>().flipX = false;

        monstersCount--;
    }
}

Monster; which is on Monster prefab;
using System.Collections;
using UnityEngine;

public class Monster : MonoBehaviour
{
    private GameObject monster;
    private Vector3 currentSpawnPoint;

    public GameObject[] spawnPoints;
    public GameObject[] destinationPoints;

    [SerializeField] float speed = 0.25f;

    bool destroy;

    void Start()
    {
        monster = this.gameObject;
        currentSpawnPoint = monster.transform.position;
    }

    void FixedUpdate()
    {
        StartCoroutine(MoveMonsterToDestination(monster, currentSpawnPoint, speed));
        //To destroy prefab upon contact with colliders at the edges of screen.
        if (destroy == true)
            Destroy(monster);
    }

    IEnumerator MoveMonsterToDestination(GameObject prefab, Vector3 firstCoord, float step)
    {
        if (firstCoord == spawnPoints[0].transform.position)
        {
            transform.position = Vector2.MoveTowards(
                                firstCoord, destinationPoints[0].transform.position, step * Time.deltaTime);
        }
        else if (firstCoord == spawnPoints[1].transform.position)
        {
            transform.position = Vector2.MoveTowards(
                                firstCoord, destinationPoints[1].transform.position, step * Time.deltaTime);
        }
        else if (firstCoord == spawnPoints[2].transform.position)
        { //If spawn p. in the middle, randomly choose where to go.
            if (Random.Range(0, 100) < 50)
            {
                transform.position = Vector2.MoveTowards(
                                firstCoord, destinationPoints[2].transform.position, step * Time.deltaTime);
            }
            else
            {
                transform.position = Vector2.MoveTowards(
                                firstCoord, destinationPoints[3].transform.position, step * Time.deltaTime);
            }
        }
        yield return new WaitForSeconds(0.5f);
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "DEAD-ZONE")
            destroy = true;
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.tag == "DEAD-ZONE")
            destroy = false;
    }
}

Thanks for taking time ^^ !

Your code is getting to the while statements in your MoveAlongY method and never exits. Since the update method is waiting for that method to finish, it will hang Unity.
You need to move the spawned objects in their update method and without while statements. Use and if statement instead.


On another note, you are also trying to move your prefab, not the spawned object.