So im having trouble accessing a variable from another script. its weird that its not working because according to all the searching i have done it should be properly pulling the target variable from a different script. I have tried coding this several different ways and nothing seems to be working for me.
What i have is a tower in my scene, when an enemy moves close enough it acquires the target and fires a projectile at it with a certain speed and direction
I want the projectile to constantly rotate towards the target since it is moving
when i try and access this variable from the other script i keep getting this error in my console
NullReferenceException: Object reference not set to an instance of an object
to me it looks like my code never actually sets the projectileTarget variable to the target variable of the tower
here is my code for review
thanks in advance
script for the projectile
var explosionPrefab : Transform;
var projectileTarget : Transform;
function OnCollisionStay()
{
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
Destroy(gameObject);
}
function Update()
{
transform.LookAt(projectileTarget);
}
function Awake()
{
var towerScript : TowerAttack;
towerScript = GetComponent("TowerAttack");
projectileTarget = towerScript.target;
}
TowerAttack script
var projectileOfTower : Transform;
var projectileSpeed = 20000;
var target : Transform;
var towerRange = 100;
var towerAttackRate = 1.0;
var doneAttacking = true;
var projectile : Transform;
function Awake()
{
//set target
target = GameObject.FindWithTag("Enemy").transform;
print("target aquired");
}
function Update ()
{
//check if the target is in range
if((Vector3.Distance(transform.position, target.position)) < towerRange)
{
//look at target
transform.LookAt(Vector3(target.position.x,0, target.position.z));
if(doneAttacking)
{
//fire
AttackTarget();
//wait till tower is off cooldown
TowerCooldown();
}//end doneAttacking
}//end Distance
}//end update
function AttackTarget()
{
// fire the projectile
projectile = Instantiate(projectileOfTower, transform.position, Quaternion.identity);
projectile.rigidbody.AddForce(transform.forward * projectileSpeed);
projectile.transform.LookAt(target);
}
function TowerCooldown()
{
doneAttacking = false;
yield WaitForSeconds(towerAttackRate);
doneAttacking = true;
}