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 CG-DJ · Aug 06, 2013 at 03:00 AM · raycastnullreferenceexceptionraycastallpathfind

NullRefException in RaycastAll

Ok, so I'm chugging through Eteeski's AI Pathfinding tutorials, trying to make the enemy's body stay on the ground, but I'm hitting an error that only appears when I run it in play mode:

 NullReferenceException: Object reference not set to an instance of an object
 Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)
 GenericEnemyBody.Update () (at Assets/Scripts/GenericEnemyBody.js:21)

And here is my code:

 var moveDamp : float = .1;
 var turnSpeed : float = 10;
 var parentToFollow : Transform;
 
 var velocityX : float;
 var velocityZ : float;
 
 function Awake () 
 {
     parentToFollow = transform.parent;
     transform.parent = null;
 }
 
 function Update () 
 {
     transform.position.x = Mathf.SmoothDamp(transform.position.x, parentToFollow.position.x, velocityX, moveDamp);
     transform.position.z = Mathf.SmoothDamp(transform.position.x, parentToFollow.position.z, velocityZ, moveDamp);
     transform.rotation = Quaternion.Lerp(transform.rotation, parentToFollow.rotation, turnSpeed * Time.deltaTime);
     
     var hits : RaycastHit[];
     hits = Physics.RaycastAll(Ray(transform.positon + Vector3(0, 100, 0), (Vector3.up * -1)));
 
     for(var i : int = 0; i < hits.length; i++)
     {
         var hit : RaycastHit = hits[i];
         if(hit.transform.gameObject == parentToFollow.gameObject.GetComponent(GenericEnemyBehavior).currentCell)
         {
             transform.position.y = hit.point.y;
         }
     }
 
 }

Thanks for the help!!!

Comment
Add comment · Show 2
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 CG-DJ · Aug 06, 2013 at 02:10 PM 0
Share

Anyone? Anyone? Anyone?

avatar image gardian06 · Aug 06, 2013 at 02:42 PM 0
Share
  • where does the errors point to (the top of the callstack)?

  • is this parentToFollow actually set to something (does this object have a parent)?

  • why in the name of performance are you doing GetComponent potentially n=inf times and in Update? (cache this component, and then jest grab value as needed)

2 Replies

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

Answer by Xtro · Aug 06, 2013 at 02:51 PM

Can you please try to call this overload instead of the one you used. You can pass float.MaxValue to distance parameter. I remember I had some issues with raycast before and I suspected that it was Unity's internal bug. Please try this and let me know if it help.

RaycastAll(ray: Ray, distance: float)

Comment
Add comment · Show 9 · 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 gardian06 · Aug 06, 2013 at 05:43 PM 0
Share

his GetComponent call is being done on an object that is being set in Awake() (meaning that I hope that object already has that component attached as it is a question of hierarchy, and that this object has a parent or not)

also as a few side notes:

  • if(gameobject==null) is the same as if(!gameObject) which are both valid, and used widely the same as if(gameObject) is the same as if(gameObject != null)

  • GetComponent() is allowed to return null, and it is completely acceptable. you just should check to see if it has something before working with it.

  • where OP has not told us what line this error occurs on this is a wild goose chase. for all we know the error could result on line10 because the object has no parent.

avatar image Xtro · Aug 06, 2013 at 05:56 PM 0
Share

Oh sorry. I thought it was a call on the Hit object.

avatar image CG-DJ · Aug 07, 2013 at 12:40 AM 0
Share

Doesn't it say in the error that the error occurs at line 21? (at Assets/Scripts/GenericEnemyBody.js:21)

If it could be anywhere, than i'll start testing things that aren't line 21. Thanks for the ideas, and get back to you!

avatar image CG-DJ · Aug 07, 2013 at 12:47 AM 0
Share

Ok, yeah. The error is definitely co$$anonymous$$g from line 21. If I take that line out, the error disappears. And if I leave the line in and take out almost everything else out, the error still appears. So the error is co$$anonymous$$g from line 21...

avatar image Xtro · Aug 07, 2013 at 02:45 AM 1
Share

The overload version without distance parameter is bugged. Now I'm sure. Thank you for proving me correct.

Show more comments
avatar image
0

Answer by gardian06 · Aug 06, 2013 at 05:51 PM

without knowing which line these errors occur on here is my stab in the dark:

  • does this object even have a parent to begin with (why is this component that is being gotten n=inf times an Update() not being cached if the object is just a parent)

  • does the list have anything in it? is the list null?

  • does the parent have this component you are trying to get n=inf times

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Broadcast Message using RaycastAll 1 Answer

RaycastHit2D.collider is null - why? 1 Answer

Check when RaycastAll leaves an object 0 Answers

How to raycast all intersection points to a PolygonCollider2D? 0 Answers

[help]RayCast check [SOLVED] 2 Answers


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