- Home /
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!!!
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)
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)
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.
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!
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...
The overload version without distance parameter is bugged. Now I'm sure. Thank you for proving me correct.
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
Your answer
Follow this Question
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