Hi, I am a fairly new Unity developer who is currently struggling to find an answer to his issue. I am guessing this question hasn’t been asked often as I could not find a similar answer that could resolve my issue.
Anyways, I am trying to make a way for my game object called Darwin to find the nearest object in the area - cookies and other Darwins. In my TriggerArea class, there are object lists called darwinsInArea and cookiesInArea that store any object within the area of the trigger collider that is attached to an Instance of Darwin.
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TriggerArea : MonoBehaviour
{
public int numCookies = 0;
public int numDarwins = 0;
public List cookiesInArea = new List();
public List darwinsInArea = new List();
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Cookie"))
{
Debug.Log("TriggerArea - Cookie");
cookiesInArea.Add(other.gameObject);
numCookies++;
Debug.Log(numCookies);
}
else if (other.gameObject.CompareTag("Darwin"))
{
Debug.Log("TriggerArea - Darwin");
darwinsInArea.Add(other.gameObject);
numDarwins++;
Debug.Log(numDarwins);
}
}
void OnTriggerExit2D(Collider2D other)
{
if(cookiesInArea.IndexOf(other.gameObject)>=0)
{
cookiesInArea.Remove(other.gameObject);
numCookies--;
Debug.Log(numCookies+"Cookie(s)");
}
else if (darwinsInArea.IndexOf(other.gameObject) >= 0)
{
darwinsInArea.Remove(other.gameObject);
numDarwins--;
Debug.Log(numDarwins+"Darwin(s)");
}
}
}
`
Then, there is another class called DarwinAI that has a GameObject method called getClosestObject that tries to find the nearest object within the lists darwinsInArea and cookiesInArea from the TriggerArea class.
Here is the DarwinAI class with only the parts I believe to be important.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DarwinAI : MonoBehaviour
{
private TriggerArea triggerArea;
public GameObject thing;
void Start()
{
triggerArea = gameObject.GetComponent<TriggerArea>();
}
void Update()
{
thing = getClosestObject(triggerArea.darwinsInArea, triggerArea.cookiesInArea);
}
GameObject getClosestObject(List<GameObject> darwins, List<GameObject> cookies)
{
float dist = float.MaxValue;
GameObject closestObject = null;
for (int i=1;i<darwins.Count;i++)
{
if (Vector3.Distance(darwins*.transform.position, transform.position) < dist)*
{
closestObject = darwins*;*
dist = Vector3.Distance(darwins*.transform.position, transform.position);*
}
}
if (closestObject == null)
{
dist = float.MaxValue;
foreach (GameObject potentialTarget in cookies)
{
if (Vector3.Distance(potentialTarget.transform.position, transform.position) < dist)
{
closestObject = potentialTarget;
dist = Vector3.Distance(potentialTarget.transform.position, transform.position);
}
}
}
return closestObject;
}
}
Here is the entire DarwinAI class.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DarwinAI : MonoBehaviour
{
private Rigidbody2D rigidBody;
private DarwinTraits traits;
private TriggerArea triggerArea;
public GameObject thing;
void Start()
{
rigidBody = gameObject.GetComponent();
traits = gameObject.GetComponent();
triggerArea = gameObject.GetComponent();
//charge(Random.Range(0.0f, 360.0f), 200.0f);
//charge(0.0f, 200.0f);
//thing = getClosestObject(triggerArea.darwinsInArea, triggerArea.cookiesInArea);
//charge(getAngle(thing), 200.0f);
//triggerArea.getClosestObject();
//charge(getAngle(thing), 200.0f);
}
void Update()
{
thing = getClosestObject(triggerArea.darwinsInArea, triggerArea.cookiesInArea);
charge(getAngle(thing), 200.0f);
}
void charge(float angle, float strength)
{
Vector3 dir = Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.right;
rigidBody.AddForce(dir * strength);
}
float getAngle(GameObject other)
{
Vector3 dir = other.transform.position - transform.position;
dir = other.transform.InverseTransformDirection(dir);
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
return angle;
}
GameObject getClosestObject(List darwins, List cookies)
{
float dist = float.MaxValue;
GameObject closestObject = null;
for (int i=1;i<darwins.Count;i++)
{
if (Vector3.Distance(darwins*.transform.position, transform.position) < dist)*
{
closestObject = darwins*;*
dist = Vector3.Distance(darwins*.transform.position, transform.position);*
}
}
if (closestObject == null)
{
dist = float.MaxValue;
foreach (GameObject potentialTarget in cookies)
{
if (Vector3.Distance(potentialTarget.transform.position, transform.position) < dist)
{
closestObject = potentialTarget;
dist = Vector3.Distance(potentialTarget.transform.position, transform.position);
}
}
}
return closestObject;
}
}
For whatever reason, when I try to set the value of the GameObject variable thing to the method getClosestObject even when there are objects within the Darwin object’s trigger collider, it says that variable thing’s reference is not set to an instance of an object.
> NullReferenceException: Object
> reference not set to an instance of an
> object DarwinAI.Update () (at
> Assets/Darwin/DarwinAI.cs:30)
> Blockquote
If anyone can solve my issue, I would be extremely grateful. Thank you to anyone who takes their time to help an amateur like me.