- Home /
 
Null reference exception on raycast collider hit? Targetter vs Moving Player
Okay so I have this code that allows this unit to target the player. It uses a ray cast script to do this. However, when I create about 5 of these units and they target the player as I'm moving around I get a null reference exception. I think this is because sometimes the ray gets blocked by another unit or something. Either way I need to figure out the root of this problem.
The error points out this code...
 if (hit.collider.gameObject==targetObj && hit.collider.gameObject !== null)
 
               here is the rest of the code...
 function Update()
 {
     var targetObj : GameObject = targetter;
     
     if(targetObj !== null)
     {
         var target : Transform = targetObj.GetComponent(Transform);
         
         //if an enemy as further than maxDistance from you, it cannot see you
         var rangeSquared = range * range;
         var rayDirection : Vector3 = target.position - transform.position;
         var enemyDirection : Vector3 = transform.TransformDirection(Vector3.forward);
         var angleDot = Vector3.Dot(rayDirection, enemyDirection);
         var playerInFrontOfEnemy = angleDot > 0.0;
         var playerCloseToEnemy =  rayDirection.sqrMagnitude < rangeSquared;
          
         if (playerInFrontOfEnemy && playerCloseToEnemy)
         { 
              inCombat = true;
             //by using a Raycast you make sure an enemy does not see you 
             //if there is a bulduing separating you from his view, for example
             //the enemy only sees you if it has you in open view
             var hit : RaycastHit;
             Physics.Raycast (transform.position,rayDirection, hit, range);
             if (hit.collider.gameObject==targetObj && hit.collider.gameObject !== null)
             {
                    inCombat = true;
                  // Look at and dampen the rotation
                 var rotation = Quaternion.LookRotation(target.position - transform.position);
                 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);    
             
                 //Once the unit is facing the target, this unit will fire
                 if(Time.time - lastFired > reloadTime && Vector3.Angle(transform.forward, target.transform.position - transform.position) < angle)
                 {
                        Fire();
                        lastFired = Time.time;
                 }
             } 
             else 
             {
                 //enemy doesn't see you
                }
            }
            else
            {
                inCombat = false;
         }
     }
 }
 
 
              Answer by robertbu · Jul 21, 2014 at 04:38 AM
The problem is that you are not checking the return value from the Physics.Raycast(). If the Raycast() fails, then hit will not have a valid collider, so your code on line 25 will generate a null reference. If the Raycast() succeeds, then the hit will contain a value game object, so the solution is to combine lines 24 and 25 into:
  if (Physics.Raycast (transform.position,rayDirection, hit, range) && (hit.collider.gameObject==targetObj))
 
               For future questions, please include a copy of the error message from the Console. It gives us the line number of the error and the stack trace.
Your answer