c # – Unity Hit Collider GameObject is Null when Collider is a child (line-of-sight detection problem)

I create a line of sight code in Unity. It allows a radar to check whether it can see an object or whether it is behind an obstacle (a collider).

As with Unity's suggestion, I check if a Linecast passing from a radar to a target hits a collider belonging to the target's game object.

In my implementation, a Target is a type whose buildings and vehicles inherit. Radar is a script attached to an object of the game, followed by the transformation of this object from the game to any Target in the range of the radar.

The method that gives me problems is below. It's called whenever a radar checks if a Collider stands between her and the Target (targets behind a wall or mountain can not be tracked).

/// 
/// Check if two objects can be seen. This does not verify any maximum distance.
/// 
/// The sighted object
/// The object seen
/// True if within sight, false if another collider is on the way
protected bool InLOS (transformation researcher, transformation target)
{
RaycastHit hit;
Physics.Linecast (seeker.position, target.position, out hit);
if (hit.collider.gameObject == target.gameObject) // Null error? Make sure that the sensor is not in a collider or rigid body, or that the trigger is activated.
return true;
other
returns false;
}

This method works to some extent. The image below shows its result. The green lines represent unobstructed radar tracking. (There are two radars in the scene: one in the plane and one in the parable).

enter the description of the image here

Note that the plane can not see the satellite dish, any more than it can see the plane (although they are both targets). As a result, there are two null errors in each image: NullReferenceException: The reference to an object is not defined on an instance of an object.

The error returns me to the following line: if (hit.collider.gameObject == target.gameObject). Indeed, unlike other, simpler targets, the aircraft does not have its collider on the parent, but many different colliders in its children.

I tried to find the parent game object in the collider, but that did not work either:

// Raycast
// ...

try
{
if (hit.collider.gameObject == target.gameObject) // Null error? Make sure that the sensor is not in a collider or rigid body, or that the trigger is activated.
return true;
other
returns false;
}
capture
{
if (hit.collider.GetComponentInParent() .transform.gameObject == target.gameObject)
return true;
other
returns false;
}

Same null error on hit.collider.GetComponentInParent() .transform.gameObject == target.gameObject.

Well, check the NULL values:

// Raycast
// ...

GameObject raycastHit = hit.collider.gameObject;
Desired GameObjectHit = target.gameObject;

if (raycastHit == null)
{
// The collider must be a Raycast target child, then
raycastHit = hit.collider.GetComponentInParent() .transform.gameObject;
}

if (desiredHit == null)
{
// The collider must be a child ...
desiredHit = target.transform.GetComponentInParent() .transform.gameObject;
}

// Check the equivalence of hits
if (raycastHit == wishedHit)
return true;
other
returns false;

No, same old reference null. The man are those GetComponent <> the methods are always painful.

So let's go easy:

if (Physics.Linecast (seeker.position, target.position))
return true;
other
returns false;

In this case, the obstacles are no longer detected. Hmm.

Question: How can I write GetComponentInParent <> correctly to find the Target the transformation of the game object, knowing that sometimes Target will be a component of the same game object as its collider, while in others, Target will be the parent, and various colliders will be children?