This script keeps crashing my unity why?

This script is supposed to move an object between two different cubes but it keeps crashing my unity no idea why i added a counter to get rid of the infinite loop but still crashes any idea i’m still pretty new to coding btw if you have a better suggestion for the movement between two cubes i would love to hear it thanks!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveAsteroid : MonoBehaviour {
    public Transform Target;
    public Transform Target2;
    public int speed;
    public int reset = 1;
    public int count = 0;


    // Use this for initialization
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        float step = speed * Time.deltaTime;
        while (reset == 1)
        {
            transform.position = Vector3.MoveTowards(transform.position, Target.position, step);
            if (transform.position == Target.position) {
                reset = 2;
                count++;
            }
        }
        while (reset == 2) {
            transform.position = Vector3.MoveTowards(transform.position, Target2.position, step);
            if(transform.position == Target2.position) {
                reset = 1;
                count++;
            }
        }
        if(count == 5)
        {
            reset = 0;
        }
    }

}

You don’t need a while in update. Just use an if or use while with a coroutine

The main problem you have (apart from the while loop) is that you’re comparing the 2 positions to see if it’s reached the destination. In floating point maths that’s not a good thing to do as they were very rarely match exactly (they may be 0.000001 out so your loop will never finish).

A better idea is to check if they are ‘close enough’. Instead of doing this:

if (transform.position == Target.position) {

you could do this:

if ((transform.position - Target.position).magnitude < 0.01f ) {

This would assume the cubes are close enough when the distance between them is less than 1cm.