using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class pRocketMod : MonoBehaviour {
//private Vector3 closestTarget;
private GameObject leftClosestTarget;
private GameObject rightClosestTarget;
//public GameObject rocket;
public GameObject leftRocket;
public GameObject rightRocket;
private float speed = 5.0f;
private float lifeTime;
public UniversalStats uStats;
public List<Collider> rCol = new List<Collider>();
void OnTriggerEnter(Collider other)
{
if (other.collider.tag == "Enemy") {
rCol.Add(other.gameObject.collider);
}
}
void RightRocketCheckDistance()
{
rightRocket.transform.position += Vector3.up * speed * Time.deltaTime;
if(rCol.Count > 0 && rightRocket != null)
{
foreach(Collider r in rCol)
{
rightRocket.transform.LookAt(r.transform.position);
rightRocket.transform.position = Vector3.MoveTowards(rightRocket.transform.position, r.transform.position,15.0f * Time.deltaTime);
if(Vector3.Distance(r.transform.position,rightRocket.transform.position) < 2.5f && r.gameObject.tag == "Enemy")
{
Destroy(r.gameObject);
rCol.Remove(r.collider);
Destroy(rightRocket);
}
}
}
}
void Update()
{
RightRocketCheckDistance ();
}
}
InvalidOperationException: Collection was modified; enumeration operation may not execute.
System.Collections.Generic.List1+Enumerator[UnityEngine.Collider].VerifyState () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:778) System.Collections.Generic.List
1+Enumerator[UnityEngine.Collider].MoveNext () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:784)
pRocketMod.RightRocketCheckDistance () (at Assets/PlayerFolder/PlayerScripts/pRocketMod.cs:36)
pRocketMod.Update () (at Assets/PlayerFolder/PlayerScripts/pRocketMod.cs:53)
MissingReferenceException: The object of type ‘BoxCollider’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Component.get_transform () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineComponent.cs:20)
pRocketMod.RightRocketCheckDistance () (at Assets/PlayerFolder/PlayerScripts/pRocketMod.cs:38)
pRocketMod.Update () (at Assets/PlayerFolder/PlayerScripts/pRocketMod.cs:53)
Those are the two errors Im getting. I’ve been trying to find ways around them, but it somehow causes more errors.
So does anyone have a solution to this?