cubes fall through floor

In my game the player has to position some cubes above slots (other cubes). These slots are placed on a plane. The player can grab the cubes with the mouse and move them around. If he releases the cubes, the cube checks if it is above a slot, and if so, the cube is normalized to the coordinates of the slot, to fit it perfectly. That usually works, but sometimes the cubes fall through the slot and the floor and I sometimes get an error on the hit collider object.

I post my release cube code. Maybe some has an idea.
(The is mostly a bunch of cubes connected, that the player moves around. Usually it happens with more cubes connected)

var hit : RaycastHit; //Saves the hitted object 

function OnMouseUp()
{
 ReleaseRightCube();
 ReleaseLeftCube();
 ReleaseCube();
}
function ReleaseCube()
{
 gameObject.renderer.material.color = originalCubeColor; //reassign original Color after dropping the Object
 aboveSlot = false;
 transform.position.y = 2;//cube lifted by 2, falling down by physics
     GetSlot();
     if(hit.collider.gameObject) //check if on a slot
     {
     if(hit.collider.gameObject.tag =="slot") //Still above a slot? 
          {
          aboveSlot = true;
           transform.position.x = hit.collider.gameObject.transform.position.x; //normalize Cube to center of the slot
           transform.position.z = hit.collider.gameObject.transform.position.z;
            }
     }
     
}
function ReleaseLeftCube()
{
 if(connectedCubeLeft)
 {
 connectedCubeLeft.GetComponent.<MovableCubeScript>().ReleaseCube();
 connectedCubeLeft.GetComponent.<MovableCubeScript>().ReleaseLeftCube();
 }
}
function GetSlot()
{
 var ray = new Ray(transform.position, (-transform.up)); //check object under the cube
 Physics.Raycast (ray, hit, 10.0);
}

Well if cubes are “snaped” to the slot, maybe just deactivate the rigidbody (rigidbody.active = false or rigidbody.isKinematic = true), as it won’t move again.

If you still want the cube to be affected by gravity, try to position it with rigidbody.MovePosition instead of moving it with transform.position (the physic engine don’t run as the same “speed” than the game, so your modification can be “ignored”)

But yeah it can also come from an exception (the error message you got, like you try to grab GetComponent(), but there isn’t one things like that…), so would be nice to have it, if you can post it.

You need to adjust settings in PhysicsManager.

Edit - Project Settings - Physics

Where you adjust “Sleep velocity”, “Sleep Angular velocity” and “Min. penetration for Penalty” until your cube falls and stays on the plane

ok, now i’ve made the slots and the plane rigidbodys to and deactivated gravity and kinematics, so it stayes where it is (does that make sense or did I destoy the whole purpose of rigidbody by doing this?)
Still I cant drop the cubes on the slots. If I drop them, they are dropped to the nearest position next to the slot???

That’s how the code looks now:

function ReleaseCube()
{
 var newPosition = Vector3(0,2,0); //cube lifted by 2, falling down by physics
 gameObject.renderer.material.color = originalCubeColor; //reassign original Color after dropping the Object
 aboveSlot = false;
 isMoving = false;
 var ray = new Ray(transform.position, (-transform.up)); //check object under the cube
 if(Physics.Raycast (ray, hit, 10.0)) //check if on a slot
    {
     if(hit.collider.gameObject.tag =="slot") //Still above a slot? 
     {
     aboveSlot = true;
    //constrain Cube to center of the slot
         rigidbody.MovePosition(Vector3(hit.collider.gameObject.rigidbody.position.x,2,hit.collider.gameObject.rigidbody.position.y));
         }
         else
          rigidbody.MovePosition(rigidbody.position+ newPosition);
     }
     else
      rigidbody.MovePosition(rigidbody.position+ newPosition);   
}

Now it occured in some tests, that the cubes fall through the plane sometimes even when I create and place them. I create the cube from a prefab and then place the at random positions over the plane. I used translate.position to arrange them - i fixed that to rigidbody Movement, but the error remains.
It also seems to happen more often on faster computers. When I’m using my computer at work (old windows core2 duo) I can hardly reproduce the error. On my 1 year old MacBook Air at home it happens quite often.

My guess at this point is, that it has something to do with the speed and at some point there is a gap between the frames, where the objects dont collide. Or- on the other hand thy collide while being created and fly around out of control.

But when I pause the game an manipulate the position of the cubes that fell through on the fly (put their coordinates back over the plane) they still fell through. They seem to have an enormous speed already.

Any suggestions?

Thanks in advance

Don’t use planes. They are known to have collision issues with moving objects.

Change your floor to a cube with a box collider.
You can also increase the overall scale of your project objects if you continue to have problems.