- Home /
adding things within certain distance into list
Hello everyone, I'm working on a prototype simple 2D pathfinder AI that randomly moves about the map. my basic premise is that there is a map template with "nodes" evenly distributed and are destroyed on startup if they are in contact with a wall, then the AI creates a list of all "nodes" within the trigger colliders radius and then removes them from the list when the exit the triggers radius. now the problem comes in when the AI wont add the "nodes" to the list saying that they aren't gameobjects.
here's the error : NullReferenceException: Object reference not set to an instance of an object
and here's the whole script:
using UnityEngine; using System.Collections.Generic;
public class Enemy_AI : MonoBehaviour {
public float TankSpeed;
public float RotationSpeed;
public GameObject Explosion;
public GameObject Dead;
private List <GameObject> waypoints;
private GameObject CurrentWaypoint;
// Use this for initialization
void Start () {
}
void OnCollisionEnter2D (Collision2D collision2D) {
if (collision2D.transform.tag == "Shell") {
Instantiate (Explosion, transform.position, transform.rotation);
Instantiate(Dead, transform.position, transform.rotation);
Destroy (gameObject);
}
}
void OnTriggerEnter2D(Collider2D entering)
{
if (entering.tag == "Waypoint")
{
waypoints.Add(entering.gameObject);
Debug.Log(waypoints);
}
}
private void OnTriggerExit2D(Collider2D leaving)
{
waypoints.Remove(leaving.gameObject);
}
// Update is called once per frame
void Update () {
transform.Translate(Vector2.up * TankSpeed * Time.deltaTime);
}
}
any help appreciated. two minds are better than one.
Your nodes, need box colliders, and at $$anonymous$$imal, thats all it needs - then it can be detected, and should be recognized as a game object (in fact, I dont even think you can create any object in Unity that can be unregistered as a game object anyway, a Transform is still a game object, but cant be detected by OnTrigger/OnCollision, or most detection methods, cause theres nothing for it to link to)
What iv done for my AI system, is use a Sphere Collider that detects other colliders (since my waypoints are only box colliders, thats the only one detected anyway), and use Vector3.Distance to find out which nodes are closest to the AI, and then a bunch of other logic to detect if they can move to the waypoint, how to move around walls and stuff that may be blocking a direct forward to that waypoint, etc...
Answer by juicyz · Jan 17, 2017 at 04:49 PM
Your list of waypoints is never initialized.
private List <GameObject> waypoints;
// Should be
private List<GameObject> waypoints = new List<GameObject>();
//OR
void Start() {
waypoints = new List<GameObject>();
}
Your answer
Follow this Question
Related Questions
AI Movement Framework Recommendations for 2D Top Down Game 0 Answers
2D Tile Road Pathfinding 2 Answers
How do I implement A* pathfinding to my 2d game, without tiles? 4 Answers
A* Pathfinding issues, player node is always off (converting 3d to 2d) 0 Answers
FixedUpdate is Being Called Less and Less Over Time...Why? 1 Answer