- Home /
Question by
ViniVidi · Mar 12, 2014 at 10:56 PM ·
raycastlinerenderernoob
Bouncing LineRenderer, null exception.
Noob question, I am trying to break a bouncing laser on specific tag but it gets Null Refrence Exception a lot. Works fine if I don't try to break out of the bounce (stops recasting if first object matches), hoping someone can point me in the right direction. It does work, but only for a bit before the error gets thrown.
void Update ()
{
nReflections = Mathf.Clamp(nReflections,1,nReflections);
ray = new Ray(goTransform.position,goTransform.up);
nPoints = nReflections;
lineRenderer.SetVertexCount(nPoints);
lineRenderer.SetPosition(0,goTransform.position);
for(int i=0;i<=nReflections;i++)
{
if(i==0)
{
if(Physics.Raycast(ray.origin,ray.direction, out hit, 100))
{
inDirection = Vector3.Reflect(ray.direction,hit.normal);
ray = new Ray(hit.point,inDirection);
if(nReflections==1)
{
lineRenderer.SetVertexCount(++nPoints);
}
lineRenderer.SetPosition(i+1,hit.point);
if (hit.rigidbody.tag == "cell")
{
break;
}
}
}
else
{
if(Physics.Raycast(ray.origin,ray.direction, out hit, 100))
{
inDirection = Vector3.Reflect(inDirection,hit.normal);
ray = new Ray(hit.point,inDirection);
lineRenderer.SetVertexCount(++nPoints);
lineRenderer.SetPosition(i+1,hit.point);
}
}
}
}
Comment
Providing the error message and/or showing the line that is generating the null will help solve this issue.