Hi guys ! I have a problem that i cant resolve myself ! Im working on a tower defense, i have my first tower done with its script, works perfectly. Im using a List to put de ennemies of the wave in. The problem is when i make a second tower ( the same as the first ) it doesnt get the ennemies in the List. They only go in the first tower’s script. I dont know why it doesnt work for the same tower…! I hope one of you can resolve this ! Thanks ! Here’s my code
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class RangedAttackAllied : MonoBehaviour
{
public List targets;
private GameObject missile;
private GameObject[] missileArray;
private Transform closestEnemy;
public float towerRange;
public float attackSpeed;
public Transform towerGunTip;
private Vector3 targetLookAt = new Vector3(0,0,0);
private float nextDamageEvent;
private float attackDelay = 5.0f;
private float dist;
void Start()
{
towerGunTip = transform;
}
public void DeselectedUnit(Transform unit)
{
targets.Remove(unit.transform);
}
void Update()
{
attackDelay += Time.deltaTime;
}
void FixedUpdate ()
{
if(targets.Count > 0)
{
targets.Sort(delegate(Transform t1, Transform t2)
{
return Vector3.Distance(t2.position, transform.position)
.CompareTo(Vector3.Distance(t1.position, transform.position));
}
);
closestEnemy = targets[0];
if(targets[0] != null)
{
targetLookAt.x = closestEnemy.transform.position.x;
targetLookAt.y = transform.position.y;
targetLookAt.z = closestEnemy.transform.position.z;
transform.LookAt(targetLookAt);
if (Vector3.Distance(transform.position, closestEnemy.transform.position) < towerRange)
{
if (attackDelay >= attackSpeed)
{
attackDelay = 0;
DOM.Instance.GetObject("banana", towerGunTip.position, towerGunTip.rotation);
missile = DOM.Instance.GetObject("banana", towerGunTip.position, towerGunTip.rotation);
}
}
}
}
}
}