Deleting an object from a list of objects from another script.

Hi everyone,

So I have 2 scripts set up. The first script is the BaseScript which handles the creation of soldier objects that will defend that base. And I have a script to handle the behavior of each individual soldiers.

When I create the soldiers I placed them to a list of GameObjects called unit to keep track of how many soldiers are currently still there. If all the soldiers are taken out, it will allow this base to be taken over by the player. The condition for this is if unit.count == 0, then I can take out the base.

The problem I’m having now is when I delete a soldier object when it collides with the player, I can’t remove it from the unit List of that specific base.

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

public class BaseScript : MonoBehaviour {

    public GameObject bases;
    public GameObject tile;

    public GameObject enemy;

    LevelManager levelManager;
    GameObject levelAccess;

    public int health;
    public SpriteRenderer color;

    public List<GameObject> unit;

    // Use this for initialization
    void Start () {
        unit = new List<GameObject>();
        for (float i = 0; i < 10; i++)
        {
            float num = (i * 1) / 10;

            float angle = num * Mathf.PI * 2;

            float x = Mathf.Sin(angle) * 2;
            float y = Mathf.Cos(angle) * 2;

            Vector3 point = new Vector3(x, y, 0) + this.transform.position;

            Instantiate(enemy, point, Quaternion.identity);
            unit.Add(enemy);
            

        }



        Instantiate(tile, new Vector2(bases.transform.position.x, bases.transform.position.y), Quaternion.identity);
        //health = levelManager.health;
        color = GetComponent<SpriteRenderer>();
        //color.color = new Color(0f, 0f, 1f, 1f);

    }

    // Update is called once per frame
    void Update () {
	
	}

    void SpawnSoldiers()
    {
        for(int i = 0; i<10;i++)
        {
            float theta = (2 * Mathf.PI / 10) * i;
            float x = Mathf.Cos(theta);
            float y = Mathf.Sin(theta);
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log("List legth" + unit.Count);
    }
}

SoldierScript
using UnityEngine;
using System.Collections;

public class EnemyController : MonoBehaviour
{
    SpriteRenderer color;
    public Transform target;
    public GameObject playerPos;
    public float moveSpeed;

    public PlayerController playerAccess;
    public BaseScript baseAccess;
    public int health;

    // Use this for initialization
    void Start()
    {
        playerAccess = GetComponent<PlayerController>();
        baseAccess = GetComponent<BaseScript>();
        //moveSpeed = 5.0f;
        color = GetComponent<SpriteRenderer>();
        color.color = new Color(0f, 0f, 0f, 1f);
    }

    // Update is called once per frame
    void Update()
    {

        
       
    }

    public void Move(Vector3 position)
    {
        transform.position = Vector3.MoveTowards(transform.position, position, moveSpeed * Time.deltaTime);
        Debug.Log("New Locatio " + transform.position);
    }
    void OnTriggerStay2D(Collider2D other)
    {
        //Debug.Log("Something entered!");
        if (other.tag == "PlayerUnit")
        {
            transform.position = Vector3.MoveTowards(transform.position, other.transform.position, moveSpeed * Time.deltaTime);
        }
    }
    

    void OnCollisionEnter2D(Collision2D other)
    {
        if(other.gameObject.tag == "PlayerUnit")
        {
            Destroy(this.gameObject);
            baseAccess.unit.Remove(this.gameObject);
        }
    }
}

Currently I keep getting the Object is not referenced to an object error from the soldier script.

Instantiate(enemy, point, Quaternion.identity);
unit.Add(enemy);
You’re instantiating a prefab, then adding the prefab to the units list. You instead need to add the newly created enemy to it:

GameObject newEnemy = Instantiate(enemy, point, Quaternion.identity) as GameObject;
unit.Add(newEnemy);