The game i am working on is about spawning ramps in front of cars. i have a simple spawning script that works fine. but i want to restrict the amount of clones i can make, was wondering how. THanks.
This how I recently solved this problem:
var thePrefab : GameObject;
var MaxObjectCount = 0;
function Update () {
if(Input.GetButtonUp("Fire2")){
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
instance.name = "boxSpawn";
MaxObjectCount += 1;
checkhit();
}
}
function checkhit(){
if(MaxObjectCount == 16){
Destroy(GameObject.Find("boxSpawn"));
MaxObjectCount -=1;
}
}
Where the var ‘MaxObjectCount’ adds one each time my Prefab spawns in the Update function, and in my ‘checkhit’ function I set it to destroy the first one I spawned after a set limit. So I can only have 15 clones at a time.
I’m fairly new and don’t know if this is the best approach, but it works. I am now working on allowing the MaxObjectCount var to speak to the prefab so I can also keep track of the info to be able to do the same when I destroy my prefab via a collision.
I hope this helped.