asteroids make framerate dip after too many have been created

I have a top down space shooter with asteroids that are randomly generated around the player in a 1000 by 1000 space. when the player gets close to the edge in any direction of the asteroid field it will create another 1000x1000 field of asteroids for the player to fly through.

so after creating 6 or so fields of asteroids the game starts to slow down. I want the player to be able to return to a spot it has previously been and everything is in the same place so I can’t just destroy a field when the player leaves it. this is a code i have tried. there’s a big trigger collider around each asteroid, so when the player gets close to one it renders. but it locks the game up at the beginning.
I’m unsure how to free up resources. Whats a good way to do this? thanks!

void OnTriggerStay(Collider other) {
	if (other.tag == "Player") {
		gameObject.renderer.enabled = true;
	} else {
		gameObject.renderer.enabled = false;


As tanoshimi mentioned, unless asteriods are very rare, the player is not going to remember what position they were in when they left an area, so how about a different approach, where you have a set field of asteroids that simply follow the player around within some distance, this way you can utilise pooling and can know that you will never exceed a certain number of asteriods, see the following code:

public int instances = 1000;
public float dist = 100;
float buffer;
Transform[] instanceArray;
public Transform instancePrefab;
float height;
Transform thisTrans;
public float timeStep = 0.2f;
Vector3 deltaPos;
Vector3 lastObjPos;

void Start () {
	thisTrans = transform;
	height = thisTrans.position.y;
	buffer = dist + (0.1f*dist);
	instanceArray = new Transform[instances];
	for(int i = 0; i < instances; i++){
		Vector2 pos = Random.insideUnitCircle * dist;
		instanceArray *= Instantiate(instancePrefab, new Vector3(pos.x, height, pos.y)+thisTrans.position, Quaternion.Euler(0, Random.Range(0f, 360f), 0)) as Transform;*
  • }*
  • lastObjPos = thisTrans.position;*
  • InvokeRepeating(“UpdatePool”, timeStep, timeStep);*

void UpdatePool(){

  • deltaPos = thisTrans.position-lastObjPos;*
  • for(int i = 0; i < instances; i++){*
    _ Vector3 dir = (instanceArray*.position - thisTrans.position);_
    if((instanceArray_.position - thisTrans.position).sqrMagnitude < (bufferbuffer)){
    * continue;
    instanceArray.position = (thisTrans.position-dir)-deltaPos;
    lastObjPos = thisTrans.position;

    You can actually have a very small amount of asteriods using this method, as long as they cover your viewing area. You could even randomise there size and speed when the position is recalculated as well around line 36 to give the effect of there being more than there are!
    Hopefully that helps