I am making a tower defense game. I want the total amount of damage dealt to be kept track of. I have a variable called scoreCount where I am storing it in a script that I have named LevelMaster that controls game states and gui updating.
var scoreCount : float = 0;
scoreText.text = "Score: "+scoreCount;
in a second script that I have for my turrets I have this damage function using raycasts
var hit : RaycastHit;
if (Physics.Raycast(rayorigin.position, rayorigin.forward, hit, rayRange, 1<<13)) {
if(hit.collider != null ){
if (hit.collider.tag == "Ground Enemy") {
hit.collider.SendMessage("TakeDamage", myDamageAmount, SendMessageOptions.DontRequireReceiver);
Debug.Log("projectile fired");
levelMaster.scoreCount += myDamageAmount;
}
else {
Debug.Log("Hit a different collider");
}
}
}
my issue is in the second script where I have levelMaster.scoreCount += myDamageAmount;
I am getting a NullReferenceException from that line.
I have var levelMaster : LevelMaster; declared at the top of the turret script so it has access to the LevelMaster script.
I want myDamageAmount to be added to the scoreCount variable every time that chunk of code dealing damge is run.
Can anyone tell me what I can do to fix this?