When a certain number of objects are touching the floor, how do i make it dissapear?

hello, i am making a game in which you must destroy buildings, you must get a certain number of cubes on the floor to pass to the next level but instead of teleporting or whatever, i want the character to fall through the floor or make the floor disapear because the next level is under it. Therefore my question is, how , when a certain amount of cubes a touching the floor, do i make the floor and cubes dissapear, Thanks for the help!

You can accomplish that by using a BoxCollider as trigger on your floor object and increment a counter whenever a cube hits the floor. Then change the level when the counter matches the nextLevel requirement.

I’ll add some steps below:

  • Add tag of “floor”, to the floor
    object
  • Add a boxCollider to the floor as a
    trigger
  • OnTriggerEnter2D (on the cube) >
    collider.gameObject.tag == “floor” >
    increment a counter on your
    GameManager script
  • GameManager > if (counter >=
    triggerNextLevel) > startNextLevel

Base off of what you’ve put if I am understanding correctly you want to make the floor of your level disappear once a certain number of objects have touched it.

To do this, attach a script to each game object you want to count, have the script check for collision between that object and the floor of the level. Once collision happens, set a boolean in that script to true to say that it has touched the floor. From here you have two options, have the object add one to a counter (which would probably be in your game’s main script) or, have your game script check every so often how many boxes have their value set to true and if that number meets the requirement do the thing to go to the next level.

Hope this helps.

run this in a new empty scene:

    //put these next three lines at very top of your script
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
   

 public int place=-10;
	bool gogame;
	GameObject g;
	Component c;
	float timer;
	Rigidbody r;
	bool b;
	int boxcount;
	public int maxboxes=5;
    bool fall;
	List<GameObject> boxes;
	int i;
	int i2;
	int killed;
	Vector3 size;
	public float rate=.5f;
	void OnGUI(){
		if (!fall) {
						GUI.skin.label.fontSize = 26;
						GUI.Label (new Rect (0, 0, 600, 300), "number of boxes on floor is:" + boxcount);
						GUI.Label (new Rect (0, 30, 600, 300), "floor can only hold: " + maxboxes);
						GUI.Label (new Rect (0, 60, 600, 300), "you have clicked the  mouse on " + killed + " boxes!!!");
				}
		GUI.skin.label.fontSize = 40;
		if (fall) {
						GUI.Label (new Rect (200, 200, 600, 300), "YOU LOOSE");
			            GUI.Label (new Rect (200, 250, 600, 300), "YOU KILLED "+killed+" boxes");
				}
	}
	void Start () {
if (!GameObject.Find ("myfloor")) {

						g = GameObject.CreatePrimitive (PrimitiveType.Cube);
						g.transform.name = "myfloor";
						g.transform.position = Vector3.zero;
						g.transform.localScale = new Vector3 (20, .5f, 20);
						g.transform.renderer.material.color = Color.green;
						Camera.main.transform.position = new Vector3 (0, 10, -20);
						Camera.main.transform.transform.rotation = Quaternion.identity;

			            g.AddComponent(this.GetType());
				        Destroy (this);}

		boxes=new List<GameObject>();
		size = transform.localScale*.5f;
		size = size + new Vector3 (.6f, .6f, .6f);
	}
	void Update () {
		if ( Input.GetMouseButtonDown (0)){ 
			RaycastHit hit; 
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
			if ( Physics.Raycast (ray,out hit,100.0f)) {
				if(hit.transform.name!="myfloor"){killed++;
					hit.transform.gameObject.transform.position=-Vector3.up*3;}
			}}
		boxcount = 0;
		i = boxes.Count;
		b = false;
		while(i>0){i--;b=false;
			if(Mathf.Abs(boxes*.transform.position.y-transform.position.y)<size.y){*

_ if(Mathf.Abs(boxes*.transform.position.x-transform.position.x)<size.x){_
_ if(Mathf.Abs(boxes.transform.position.z-transform.position.z)<size.z){
b=true;}}}
if(!b){
i2 = boxes.Count;
while(i2>0){i2–;
if(Mathf.Abs(boxes.transform.position.y-boxes[i2].transform.position.y)<1.5f){
if(Mathf.Abs(boxes.transform.position.x-boxes[i2].transform.position.x)<1.5f){
if(Mathf.Abs(boxes.transform.position.z-boxes[i2].transform.position.z)<1.5f){
if(i!=i2){b=true;}}}}*_

* }}*
* if(b){boxcount++;}*
* }*

* if(!fall){if (boxcount > maxboxes) {*
* fall = true;*
* r=gameObject.AddComponent();*
_ r.AddForce(-Vector3.up200);
}}
if(!fall){
timer += Time.deltaTimerate;

* if (timer > 1) {timer = 0;rate=rate+.02f;
i = boxes.Count;
while(i>0){i–;
if(boxes.transform.position.y<-20){boxes.RemoveAt(i);}
}
g = GameObject.CreatePrimitive (PrimitiveType.Cube);
g.transform.localScale=new Vector3(1.2f,1.2f,1.2f);
g.transform.position=new Vector3((float)place,15,0);
r= g.AddComponent();
boxes.Add(g);
place=UnityEngine.Random.Range(-11,12);
}}*_

Put that on your floor gameobject, and tag the cubes as “Rubble”:

using UnityEngine;    

public class FloorBreak : MonoBehaviour {

    //Variables
    public int RubbleOnTheGround;
    public int LimitOfRubble;

    void OnCollisionEnter(Collision col)
    {

        if (col.gameObject.tag == "Rubble")
        {
            RubbleOnTheGround++;
        }
        if (RubbleOnTheGround >= LimitOfRubble)
        {
            Destroy(this.gameObject);
        }

    }

}

do you guys mind putting the andwer in c# for me plz? thanks for the help! @TheDJBuntin @Mercbaker