x


2 towers, same script, 1 problem

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);
               }            
         }
       }
    }
}

}

more ▼

asked May 12 '12 at 07:39 PM

Stonemove420 gravatar image

Stonemove420
22 2 4 8

To be clear - when you add the script to the second tower are you also setting the list of targets in that one? Or are you wanting the one list to be available to both of the towers?

May 13 '12 at 04:56 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I suggest you create a GameManager class that's attached to an empty game object. Use this to store the enemies in a List.

When you instantiate your tower just get the enemy list from the GameManager class.

more ▼

answered May 13 '12 at 06:20 PM

Meltdown gravatar image

Meltdown
5.6k 18 25 49

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3323
x662

asked: May 12 '12 at 07:39 PM

Seen: 205 times

Last Updated: May 13 '12 at 06:20 PM