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 Brett_MMU · May 23, 2012 at 04:49 PM · gameobjectreferencenullexception

Null reference exception on GameObject

Hi,

I'm making an AI movement script, which just takes the enemy from one waypoint to another. And it works fine, the enemy moves from one waypoint to another, randomly picks another waypoint and begins to travel to it. However i'm constantly getting thrown Null Reference Exceptions

The code is below

 using UnityEngine;

using System.Collections;

public class AImovement : MonoBehaviour {

 public GameObject[] waypoints;
 private bool move = true;
 private GameObject cWay;
 private Vector3 pos, wTarget;
 public float speed = 0.5f;
 public float captureRadius = 1;

 void Start () {
     
     //Find which waypoint is closest to player
     float dd, tempx, tempz,dx,dz, cd=1000;
     Vector3 cur = transform.position;
     for (int i=0; i<waypoints.Length; i++)
     {
         tempx = waypoints[i].transform.position.x;
         tempz = waypoints[i].transform.position.z;
         dx = Mathf.Abs (cur.x-tempx);
         dz = Mathf.Abs (cur.z-tempz);
         dd = Mathf.Sqrt ((dx*dx)+(dz*dz));
         if (dd<cd)
         {
             cd = dd;
             cWay = waypoints[i];
             Debug.Log (wTarget + " " + i);
         }
     }
 }
 
 void Update () {
     
     //Do we want to move?
     if (move)
     {
         wTarget = cWay.transform.position;
         pos = transform.position;
         
         wTarget.y = pos.y;
         transform.LookAt (wTarget);
         transform.Translate (new Vector3(0,0,speed), Space.Self);
         
         //Have we reached the target?
         if (Vector3.Distance (wTarget,pos) < captureRadius)
         {
             //Find available waypoints from current waypoint
             GameObject[] tempWay = cWay.GetComponent<waypoint>().attached;
             int j = 0;
             foreach (var s in tempWay)
             {
                 j++;    
             }
             Debug.Log ("Reached");
             cWay = tempWay[Random.Range(0, j)];                
         }
         
     }
 
 }
 
 //External way of modifying the movement
 public void movement(bool m)
 {
     move = m;    
 }

}

so cWay is the waypoint I want to travel to, and the exception is gettings thrown on line 39 which is

wTarget = cWay.transform.position;

but I set cWay in my start method, and cWay must be set anyway as the enemy travels to the next waypoint. All the waypoints are empty gameobjects which are added to waypoints[]. And each waypoint has a script with a GameObjects array called attached, which holds the way points that can be to travelled to next.

I've really got no idea and any help would be much appreciated, thanks,

Brett

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AchillesWF · May 23, 2012 at 05:12 PM

If nothing stops .attached from potentially holding a null value in the array, then you will get cWay being null. Perhaps when setting up the attached list for one of your waypoints you didn't drag a waypoint into one of the slots? You really should be protecting against this anyway in the line "if (move)" by using something like "if ( (move) && (cWay != null) )".

Also, note that if dd is not < cd, cWay will not have a value on Start.

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

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

Accessing other GameObjects Script Variables 2 Answers

Null reference Exception - but it's all there 1 Answer

Despawner script returns null reference exception. 0 Answers

Why is this code getting NullReferenceException 2 Answers

NullReferenceException Object reference error 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