Static Towers shooting incorrectly

Hello,
I’m new to Unity and it doesn’t seem like I’m alone. As quiet a few others I started with a Tower Defence project and now I’m stuck.

Tower
I’ve created a tower which is shooting at the closest target and follows it until the target leaves the trigger collider and than switches over to the now closest target and begins to shot at that one and it’s working through a List which adds a target whenever a target enters the trigger collider of the tower.

Enemy
The enemy is a really basic object it moves over a baked plane towards a waypoint and is pathing through the towers trigger collider as a very simple test.

Problem
The tower seems to work fine and the enemies can’t really work bad as there is really nothing there to break things. But when I move the tower to another position or create a second instance (duplicate) of the tower the shooting starts to get really weird. It seems like the towers .position is stuck at where I created the first tower and is aiming from that position.

I would really like someone to help me out or point me in a direction on what to look for because this is getting frustrating.

Video of problem
Video of the problem recorded from Unity

Script of the Tower(s)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TowerAttack : MonoBehaviour {

	public float 	timeBetweenAttack = 0.5f;
	public int 		attackDamage = 10;
	public float	turretTurnSpeed = 3.5f;
	public float 	firePauseTime = 0.25f;
	public float 	errorAmount = 2f;
	public float 	reloadTime = 2f;
	
	public Transform 	turretPivot;
	public Transform 	muzzlePosition;
	public GameObject 	towerProjectile;

	float aimError;
	float timer;
	
	private List<Transform> arrEnemyInRange = new List<Transform>();
	public Transform 		enemy;
	private Quaternion 		desiredRotation;


	void OnTriggerEnter(Collider other){
		if (other.gameObject.tag == "Enemy") {

			// Add object which enters collider to List
			arrEnemyInRange.Add (other.transform);

			// If there is only one item in the List set it as target
			if(arrEnemyInRange.Count == 1) {
				enemy = GetClosestEnemy(arrEnemyInRange);
			} 
		}
	}

	void OnTriggerExit(Collider other){
		if (other.gameObject.tag == "Enemy") {

			// Remove object which leaves the collider from List
			arrEnemyInRange.Remove(other.transform);

			// If the List has objects in it set the closest as target
			if(arrEnemyInRange.Count != 0) {
				// TO-DO Turret turn delay
				enemy = GetClosestEnemy(arrEnemyInRange);
			} else {
				enemy = null;
				arrEnemyInRange.Clear();
			}				      
		}
	}


	void Update(){
		timer += Time.deltaTime;

		// Aim
		if (arrEnemyInRange.Count != 0) {
			// Where to aim
			CalculateAimPosition(enemy.position);
			// Rotate the turrent towards the target
			turretPivot.rotation = Quaternion.Lerp (turretPivot.rotation, desiredRotation, Time.deltaTime*turretTurnSpeed);
		}

		// FIRE

		if (timer >= timeBetweenAttack && arrEnemyInRange.Count != 0) {
			Attack ();
		}
	}

	void Attack(){
		// Weapon delay reset
		timer = 0f;

		// Fire Projectile
		CalculateAimError ();
		
		Instantiate (towerProjectile, muzzlePosition.position, muzzlePosition.rotation);
		audio.Play ();
	}

	void CalculateAimPosition(Vector3 targetPosition){
		// Aim at the targetPosition
		Vector3 aimPoint = new Vector3 (targetPosition.x+aimError, 0, targetPosition.z+aimError);
		desiredRotation = Quaternion.LookRotation (aimPoint);
	}

	void CalculateAimError(){
		aimError = Random.Range (-errorAmount, errorAmount);
	}

	public Transform GetClosestEnemy(List<Transform> enemies){
		Transform target = null;
		float closestDistanceSqr = Mathf.Infinity;
		Vector3 currentPosition = transform.position;

		foreach (Transform potentialTarget in enemies) {
			Vector3 directionTotarget = potentialTarget.position - currentPosition;
			float dSqrToTarget = directionTotarget.sqrMagnitude;

			if(dSqrToTarget < closestDistanceSqr) {
				closestDistanceSqr = dSqrToTarget;
				target = potentialTarget;
			}
		}

		return target;
	}
	
}

First of all thank you for the wonderfully written question. You have described your issue properly, included necessary code and even a video,it very much appreciated.

You have a very well written script there. The issue I see is that in your calculate aim position, you are not using the towers position in any way. I assume that the tower that is firing correctly is probably in the center of the world and therefore does not need to take the position into account (it would be 0,0,0) to fire correctly.

You create an aimPoint and then a lookRotation which looks at that aimPoint, but if it only uses the enemy position the lookRotation works as if it is looking from a position that is at 0,0,0. So you need to calculate the aimPoint relative to the tower. This is easily done by creating a vector that points from the tower to the target:

void CalculateAimPosition(Vector3 targetPosition)
{
    // Aim at the targetPosition
    // TargetPosition substracting towerPosition creates a vector pointing from the tower to the targetPosition. 
    Vector3 aimPoint = new Vector3 (targetPosition.x+aimError, 0, targetPosition.z+aimError) - transform.position;
     desiredRotation = Quaternion.LookRotation (aimPoint);
}

Good luck with the rest of your project, tower defense games are fun to make. =)