Moving Sprite to nearest spawning object

Daniel 1 Reputation point

So for Unity I am trying to have a sprite in a scene move/teleport to the nearest spawning object to destroy it. What's supposed to happen is the teleportation and the destruction of the object but the sprite freezes after since I'm not done with everything.

Here's the class responsible for that
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// A collecting game object
/// </summary>
public class Collector : MonoBehaviour
    #region Fields

    // targeting support
    SortedList<Target> targets = new SortedList<Target>();
    Target targetPickup = null;

    // movement support
    const float BaseImpulseForceMagnitude = 2.0f;
    const float ImpulseForceIncrement = 0.3f;

    // saved for efficiency
    Rigidbody2D rb2d;


    #region Methods

    /// <summary>
    /// Use this for initialization
    /// </summary>
    void Start()
        // center collector in screen
        Vector3 position = transform.position;
        position.x = 0;
        position.y = 0;
        position.z = 0;
        transform.position = position;

        // save reference for efficiency
        rb2d = GetComponent<Rigidbody2D>();

        // add as listener for pickup spawned event


    private void DelegateMethod(GameObject arg0)
        print("Pickup Spawned");

    /// <summary>
    /// Called when another object is within a trigger collider
    /// attached to this object
    /// </summary>
    /// <param name="other"></param>
    void OnTriggerStay2D(Collider2D other)
        // only respond if the collision is with the target pickup
        if (other.gameObject == targetPickup.GameObject)
            // remove collected pickup from list of targets and game

            // go to next target if there is one


    /// <summary>
    /// Sets the target pickup to the provided pickup
    /// </summary>
    /// <param name="pickup">Pickup.</param>

    void SetTarget(GameObject pickup)
        targetPickup = pickup;

    /// <summary>
    /// Starts the teddy bear moving toward the target pickup
    /// </summary>
    void GoToTargetPickup()
        // calculate direction to target pickup and start moving toward it
        Vector2 direction = new Vector2(
            targetPickup.transform.position.x - transform.position.x,
            targetPickup.transform.position.y - transform.position.y);
        rb2d.velocity =;
        rb2d.AddForce(direction * BaseImpulseForceMagnitude, 


However my sprite is just stationary and sits there while the in-game objects spawn

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
3,782 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dylan Zhu-MSFT 6,381 Reputation points

    Hi DanielYoo ,

    The question is more related to unity development, which is not supported in Microsoft Q&A forum. Please go to Unity forum, and the community members will provide dedicated support for you.

    Best Regards, Dylan

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our * *documentation* to enable e-mail notifications if you want to receive the related email notification for this thread.**

    0 comments No comments