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;
}
}