简体   繁体   English

GameObject 没有从列表中删除?

[英]GameObject not being removed from List?

I've tried 2/3 different ways of removing my gameObject from my List but none are working.我已经尝试了 2/3 种不同的方法来从我的列表中删除我的游戏对象,但都没有用。 When I debug the method the debug log is showing up as it should yet the gameobject still isn't removed from the list.当我调试该方法时,调试日志按应有的方式显示,但游戏对象仍未从列表中删除。

When my teammates kill an enemy I want the enemy to be removed from the list and then destroyed so I can continue to iterate through the List to find the closest enemy to begin attacking.当我的队友杀死一个敌人时,我希望将敌人从列表中移除然后销毁,这样我就可以继续遍历列表以找到最近的敌人开始攻击。 Because the gameObject's are not being removed I get a null reference and i can loop through my for loop to check.因为游戏对象没有被删除,所以我得到了一个 null 参考,我可以通过我的 for 循环来检查。

1st Script: List is created and used in a for loop, removing and destroying the enemy also occurs in here.第一个脚本:列表在for循环中创建和使用,移除和摧毁敌人也发生在这里。

    public class FriendlyManager : MonoBehaviour
{
    public NavMeshAgent navMeshAgent;
    public Transform player;

    public static FriendlyManager singleton; 

    public float health;
    public float minimumDistance;

    public int damage;

    public List<GameObject> enemies;
 

    private GameObject enemy;
    private GameObject enemyObj;

    // animations
    [SerializeField] Animator animator;
    bool isAttacking;
    bool isPatroling;

    // attacking
    [SerializeField] Transform attackPoint;
    [SerializeField] public GameObject projectile;
    public float timeBetweenAttacks;
    bool alreadyAttacked;

    private void Awake()
    {
        navMeshAgent = GetComponent<NavMeshAgent>();

        animator = GetComponent<Animator>();

        enemyObj = new GameObject();
    }

    private void Start()
    {
        singleton = this;

        isAttacking = false;
        isPatroling = true;
        animator.SetBool("isPatroling", true);
    }

    private void Update()
    {

        for(int i = 0; i < enemies.Count; i++)
        {
            if(Vector3.Distance(player.transform.position, enemies[i].transform.position) <= minimumDistance)
            {
                enemy = enemies[i];
                Attacking(enemy);
            }
        }
    }

    private void Attacking(GameObject enemy)
    {
        // stop enemy movement.
        navMeshAgent.SetDestination(transform.position);

        enemyObj.transform.position = enemy.transform.position;

        transform.LookAt(enemyObj.transform);

        if (!alreadyAttacked)
        {
            isAttacking = true;

            animator.SetBool("isAttacking", true);
            animator.SetBool("isPatroling", false);

            Rigidbody rb = Instantiate(projectile, attackPoint.position, Quaternion.identity).GetComponent<Rigidbody>();
            rb.AddForce(transform.forward * 32f, ForceMode.Impulse);

            alreadyAttacked = true;
            Invoke(nameof(ResetAttack), timeBetweenAttacks);

        }
    }

    private void ResetAttack()
    {
        alreadyAttacked = false;
        animator.SetBool("isAttacking", false);
    }

    public void DestroyEnemy(GameObject enemy)
    {
        enemies.Remove(enemy);
        Debug.Log("AHHHHHHH M GOING CRAZY");
        Destroy(gameObject);
    }
    }
}

2nd Script: Deals with the damage and checks enemy's currentHealth.第二个脚本:处理伤害并检查敌人的当前生命值。 (I have to post it as an image because for Stack Overflow is being annoying.)._. (我必须将它作为图像发布,因为 Stack Overflow 很烦人。)。 在此处输入图像描述

Shouldn't it just be不应该只是

Destroy(enemy);消灭(敌人);

and not并不是

Destroy(gameObject);销毁(游戏对象);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM