Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 lawrence-parry · Jun 18, 2014 at 11:49 AM · erroraiforeach

Null Reference exception, in foreach loop in AI

I am making a jet boating game and I have been working on the AI. The way it works is it should steer towards the furthest away waypoint within it's line of sight so as to take the smoothest line. The problem is that when I run the game it says "NullReferenceException: Object reference not set to an instance of an object" for line 20 and I have no idea what the problem is.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class AI : MonoBehaviour {
     Transform targetRotation;
     //variable for target to stear towards.
     public Transform target;
     Vector3 guyInMyCoords;
     bool onWater;
     //creates a list of waypoints for the path for the boat to follow.
     List <GameObject> path;
 
     void Start () {
         path.AddRange(GameObject.FindGameObjectsWithTag ("waypoint"));
     }
     void Update () {
         float Furthest = 0;
 
         foreach (GameObject obj in path) {
     
             Transform direction = null;
             direction.LookAt (obj.transform);
 
             RaycastHit hit;
             Physics.Raycast (transform.position, direction.forward, out hit, Vector3.Distance (obj.transform.position, transform.position));
             Debug.Log (hit.collider.tag);
         
             if (hit.collider.tag != "terrain") {
 
                 var distance = Vector3.Distance (obj.transform.position, transform.position);
 
                 if (distance > Furthest) {
                     Furthest = distance;
                     //sets the target for it to stear towards.
                     target = obj.transform;
                     }
                 }
         }
         if (onWater)
         {
             rigidbody.drag = 1.8f;
 
         } 
         else{
             rigidbody.drag = 0.2f;
             
         }
         onWater = false;
 
         //this stuff controls how it steers towards the target.
 
         Quaternion sup = transform.rotation;
         
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward, Vector3.up), Time.deltaTime * 3);
         
         transform.eulerAngles = new Vector3(sup.eulerAngles.x,sup.eulerAngles.y,transform.eulerAngles.z);
 
         guyInMyCoords = target.position;
          guyInMyCoords = transform.InverseTransformPoint(target.position);
         if (guyInMyCoords.x > 0)
         {
             rigidbody.AddRelativeTorque (Vector3.up * 15);
             rigidbody.AddRelativeTorque (Vector3.forward* -0.5f);
         }
         else 
         {
             rigidbody.AddRelativeTorque (Vector3.up * -15);
             rigidbody.AddRelativeTorque (Vector3.forward* 0.5f);
         }
     }
     void OnCollisionStay(Collision other)
     {
         
         if (other.gameObject.tag == "water") 
         {
             rigidbody.AddRelativeForce (Vector3.forward* 140);
             onWater = true;
 
         }
     }
     void OnTriggerStay(Collider other)
     {
         if(other.tag == "waypoint")
         {
             path.Remove(other.gameObject);
         }
     }
 }
 

any help would be great. I am fairly new to unity so any tips or advice would be much appreciated. Thank you.

Comment
Add comment · Show 5
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 tanoshimi · Jun 18, 2014 at 11:51 AM 0
Share

Line 28? That's a blank line in the listing above. Can you check your code and paste what the actual offending line is?

avatar image lawrence-parry · Jun 19, 2014 at 04:00 AM 0
Share

sorry, I removed a few empty lines before posting this, it's actually line 20 that has the error.

avatar image Rob-Fireproof · Jun 19, 2014 at 09:30 AM 0
Share

Ah, right. You never initialise your path list. Try changing your start function to...

 void Start () 
 {
     path = new List<GameObject>();
     path.AddRange(GameObject.FindGameObjectsWithTag ("waypoint"));
 }
avatar image lawrence-parry · Jun 20, 2014 at 04:35 AM 0
Share

still hasn't fixed things, I get the same error but now on line 23.

avatar image HarshadK · Jun 20, 2014 at 05:55 AM 0
Share

Did you check if there are actually any game objects present in your list?

2 Replies

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

Answer by lawrence-parry · Jun 20, 2014 at 05:58 AM

ok, so after a bit of searching I found what the problem is, you can't create an empty transform, so instead I had to use a vector3.

Comment
Add comment · 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
0

Answer by Rob-Fireproof · Jun 18, 2014 at 02:38 PM

I think it's probably this line: Debug.Log (hit.collider.tag);

You haven't checked it's actually hit anything, so the collider could be null.

Physics.Raycast returns a bool to say whether it hit anything at all, so you should do...

 if (Physics.Raycast (transform.position, direction.forward, out hit, Vector3.Distance (obj.transform.position, transform.position)))
     {
         Debug.Log (hit.collider.tag);
         ...

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 lawrence-parry · Jun 19, 2014 at 04:03 AM 0
Share

doesn't seem to have fixed things, I still get the same error.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

stupid errors i can't figure out 1 Answer

How to make the enemy choose only one cover? 1 Answer

The generic list blues (Argument out of range error) 1 Answer

Argument out of range. 1 Answer

My enemy AI cant move? 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