[Solved] My custom terrain generation plugin instantiates to much prefabs


Firstly, change your 25 lines of Instantiation to this:

for (int i = -2; i< 3; i++)
    {
        for (int j = -2; j < 3; j++)
        {
            Instantiate(terrains[Random.Range(0,terrains.length)], 
                          Vector3(j*50, 0, i*50), Quaternion.identity);
        }
    }

Secondly, you already have the gameobject in this call, so don’t use GameObject.Find() //it’s expensive
So assuming the object that is tagged “Player” has the FPS Controller, change:

if(col.gameObject.tag == "Player") {
    this.name = "Chunk (" + this.transform.position.x + ", " + this.transform.position.y + ", " + this.transform.position.z + ")";
    gameObject.Find("First Person Controller").SendMessage("callGenerate");
}

To:

if(col.gameObject.tag == "Player") {
    this.name = "Chunk (" + this.transform.position.x + ", " + this.transform.position.y + ", " + this.transform.position.z + ")";
    col.gameObject.SendMessage("callGenerate");
}

Finally, it looks like from your code that you want to have your generation based off of where the player currently is. Rather than do this in the OnTriggerExit, put it in the OnTriggerEnter() and then pass the trigger object to you function, saving you from needing to find the active object in the callGenerate method:

function OnTriggerEnter (col : Collider) {
if(col.gameObject.tag == "Player") {
    this.name = "ACTIVE_Chunk (" + this.transform.position.x + ", " + this.transform.position.y + ", " + this.transform.position.z + ")";
    col.gameObject.SendMessage("callGenerate", this.gameObject);
    }   
}

function callGenerate(go: GameObject) {
    if(canGenerate) {
        canGenerate = false; //you were redeclaring this variable here, 
        //which is mostly likely why you had more generation going on than desired
        generateFromMiddleOfChunk(go.transform.position);
    }
}

I can’t find where it is that you are saving your chunks, but you will want to do this in a way that you only generate the new chunks that are needed.

solved My custom terrain generation plugin instantiates to much prefabs