Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Redbobster · Jan 17, 2017 at 04:37 PM · 2daipathfinding

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dibbie · Jan 17, 2017 at 05:01 PM 0
Share

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...

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Redbobster · Jan 17, 2017 at 04:54 PM 0
Share

thanks a bunch

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges