Turn Gravity on when collision occurs

Hi Everyone,

I have a FPS style game setup with cubes everywhere. When a player comes into contact with a cube a script counter begins to count down, when it reaches “0”, the cubes gravity setting is turned on and force is applied to the cube. However currently the blocks are staying static.

!

Here’s the code for the player:
using UnityEngine;
using System.Collections;

public class PlayerCCollision : MonoBehaviour {

	void OnControllerColliderHit(ControllerColliderHit hit){
		if (hit.normal.y < 0.9){ // filter out ground collisions
			// if the other object has PlayerDetect script...
			CubeFall otherScript = hit.gameObject.GetComponent<CubeFall>();
			if (otherScript){ // call its function PlayerHit
				otherScript.PlayerHit(hit);
			}
		}
	}
}

And the Code for the cubes:

using UnityEngine;
using System.Collections;

public class CubeFall : MonoBehaviour {
	float timeRemaining = 10.0f;
	bool collidedWithPlayer = false;


	void Countdown()
	{
	//	Debug.Log ("Countdown called");
		timeRemaining -= Time.deltaTime;
		if (timeRemaining <= 0.0f)
		{		
			Debug.Log ("Enable Gravity");
			rigidbody.useGravity = true;
			rigidbody.AddForce (Vector3.down * 10);
		}
	}

	public void PlayerHit(ControllerColliderHit hit){
		Debug.Log("Player Hit");
		collidedWithPlayer = true;
	}


	void Update () {
	if (collidedWithPlayer.Equals(true))
		{
			Countdown ();
		}
	}

}

So far all that happens is Debug.Log says that a “Player Hit” occurred (growing number as the game goes on) and the same with “Enabled Gravity”. Can anyone tell me why please?

Your rigidbody on the cubes is set to is kinematic. This means that it won’t react to physics. Either set iskinematic to false in the inspector or if you want them to stay kinematic until they are hit you will just have to set the iskinematic to false with script during runtime at the same time as the gravity is set to true.