Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Adding targets to an array

Discussion in 'Scripting' started by Tharkis, Mar 11, 2012.

  1. Tharkis

    Tharkis

    Joined:
    Mar 7, 2012
    Posts:
    16
    So I'm following the Bergzerg tutorials and I'm having a bit of an issue. For the life of me I cannot see what is missing. I have all my enemies tagged as Enemy and have the following code to find them but they are not adding to my targets.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class Targeting : MonoBehaviour {
    7.    
    8.     public List<Transform> targets;
    9.    
    10.     // Use this for initialization
    11.     void Start () {
    12.         targets = new List<Transform>();
    13.     }
    14.     //Add enemies to array
    15.     public void AddAllEnemies(){
    16.         GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
    17.        
    18.         foreach(GameObject enemy in go)
    19.             AddTarget(enemy.transform );
    20.     }
    21.     // Add enemies to player target
    22.     public void AddTarget(Transform enemy){
    23.         targets.Add(enemy);
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.    
    29.     }
    30. }
    31.  
     
  2. Tharkis

    Tharkis

    Joined:
    Mar 7, 2012
    Posts:
    16
    Got my answer on the unity answers page. I'm not calling the AddAllEnemies method in my start method