A problem with finding closest object and then following it

I’m creating a simulator in 2D, where a creature needs to find a closest mineral (or pellet, as I call it) then go to it, eat it, then find the next one, and so on.
Using this script, it finds the closest one, eats it, but then can’t find closest pellet from there on, and i get a ton of "MissingReferenceException"s
The problem, as i tested, is in finding the closest pellet second time. I forcefully pointed at selected pellets in Editor, and it did its job just fine.

Explanation of the script:
In “Start”
it gets the pellet array (called “minerals”) from another script
Then in start it resets the position of the creature
Then for each pellet in the array we find if it’s the closest one, and if it is, we store it in the “pellet” variable

In “movement”
we again check if the pellet we’re trying to access is missing, and if it is, we again find the closest one (i was afraid to use this as a separate function)
only then we move the creature towards the pellet, using Vector2.MoveTowards

In “OnTriggerEnter2D”
we destroy the pellet we just touched, and find the closest one again

Please send help…

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

public class creatureBehaviour : MonoBehaviour {

       

    public Rigidbody2D rb;
    public GameObject controller;
    public GameObject pellet;
    public GameObject[] pelletArray;

    public spawnPellets script1;

    private int i;
    public int pelletK = 1280;

    public float speed = 15;
    const float clos = 0.1f;
    private float energy = 20;
    private float offset;
    private float minoffset;

    Vector3 LookDirection;

    void Awake () {
        script1 = controller.GetComponent<spawnPellets> ();
        pelletArray = script1.minerals;
    }

    void Start () {
        rb = GetComponent<Rigidbody2D> ();
       
        transform.position = new Vector3 (0.0f, 0.0f, 0);

        foreach (GameObject tempTransform in pelletArray) {
            offset = (tempTransform.transform.position - transform.position).magnitude;
            if (offset < minoffset) {
                pellet = null;
                pellet = tempTransform;
                minoffset = offset;
            }
            if (minoffset == 0) {
                minoffset = offset;
            }
        }
        Debug.Log (pellet.name);
    }

    void FixedUpdate () {
       
        //movement
        if (pellet == null) {
            pelletArray = script1.minerals;
            foreach (GameObject tempTransform in pelletArray) {
                if (tempTransform == null) {
                    continue;
                }

                offset = (tempTransform.transform.position - transform.position).magnitude;
                if (offset < minoffset) {
                    pellet = null;
                    pellet = tempTransform;
                    minoffset = offset;
                }
                if (minoffset == 0) {
                    minoffset = offset;
                }
            }
            LookDirection = (pellet.transform.position - transform.position);
        }
        transform.position = Vector2.MoveTowards(transform.position, pellet.transform.position, speed * Time.deltaTime);
    }

    void OnTriggerEnter2D (Collider2D other) {
        if (other.gameObject.CompareTag ("Mineral")) {
            Destroy (other.gameObject);

            foreach (GameObject tempTransform in pelletArray) {
                if (tempTransform == null) {
                        continue;
                }

                offset = (tempTransform.transform.position - transform.position).magnitude;
                if (offset < minoffset) {
                    pellet = null;
                    pellet = tempTransform;
                    minoffset = offset;
                }
                if (minoffset == 0) {
                    minoffset = offset;
                }
            }
            LookDirection = (pellet.transform.position - transform.position);
        }
        return;
    }
}

I took a quick look and read your introduction, and I wonder if maybe you should avoid resetting the array (in FixedUpdate). Instead, just remove from your local copy (perhaps use a list, instead, for the local*).

In addition to that, I have a suggestion for you… You could use .Sort() on the array/list rather going through it yourself. Then just take the first/last (depending on how you sorted it). I would say your best choice would be sqrMagnitude as a comparer. :slight_smile:

Thanks a lot! Using lists instead of arrays helped to solve a ton of issues and has given me a lot of ideas!

Cool, glad I could help :slight_smile: