Raycast, from child, to parents var transform

I have a problem here with a raycast from a child of a parent object

var currentTarget : Transform = transform.parent; //giving me errors
var range :float = 15;

function Start () 
{

}

function Update()
{
  var hit : RaycastHit;
  
  if(Physics.Raycast (transform.position, transform.forward, hit, range))
    {
      if(hit.collider.gameObject.currentTarget) //not sure how to type this
        {
          print ("hitting current target");
        }
    }
  else  
    {
      print("Hitting nothing");
    }
}

What I am trying to do is: cast a raycast from a empty gameobject “z pointing forward” that is a child to my turrets pivot object. I require it to hit only my turrets target “curretnTarget” which is a transform variable the parent object. If that is true I want it to do something. Like turn on a boolean in my turret script which is the first parent object.

var currentTarget : Transform = transform.parent;

Variables can’t be initialized to non-static values or references. Since transform.parent actually gets compiled into this.transform.parent the compiler throws an error because you are trying to assign a non-static value (A reference to this Transform’s parent) when the Component that currentTarget belongs to hasn’t been instantiated yet. The compiler can’t pull this.transform.parent because this doesn’t exist during compile time.

Instead initialize currentTarget to null and assign the parent Transform in void Awake():

Transform currentTarget = null;

public void Awake()
{
	currentTarget = this.transform.parent;
}

if (hit.collider.gameObject.currentTarget)

The variable currentTarget isn’t a member variable of GameObject so the compiler will throw an error when it sees this (and even if it was this statement is only checking for existence and not equivalence). Instead just replace the nested if-statement with the && operator in the upper-level statement and check to see if hit.collider.gameObject is equal to currentTarget:

if (Physics.Raycast(transform.position, transform.forward, out hit, range) && (hit.collider.gameObject == currentTarget))

Here’s what the whole thing looks like after making these two changes:

private Transform currentTarget = null;
private float range = 15f;

public void Awake()
{
	currentTarget = transform.parent;
}

public void Update()
{
	RaycastHit hit = new RaycastHit();

	if(Physics.Raycast(transform.position, transform.forward, out hit, range) && (hit.collider.gameObject == currentTarget))
	{
		print("hitting current target");
	}
	else
	{
		print("Hitting nothing");
	}
}

this is more of what I was looking for:

var inSight : boolean;
var rayCastAimLocation : Transform;

function RayCastingCheck()
{
  var hit : RaycastHit;
  
  if(Physics.Raycast (rayCastAimLocation.position, rayCastAimLocation.forward, hit, range))
    {
      if(hit.transform.tag == "Enemy")
        {
          print ("hitting current target");
          inSight = true;
          
        }
      else
        {
          print("Hitting Something Else");
          inSight = false;
        }
    }
  else 
    {
      inSight = false;
      print("Hitting Nothing!!!");
    }
}