Terrain Generation ruins collision

I have a working terrain generation script,

using UnityEngine;

public class TerrainGenerate : MonoBehaviour {

public int Width = 256;
public int Height = 256;
public int Depth = 20;

public float offsetX = 100f;
public float offsetY = 100f;

public float scale = 20f;
//void Start(){
	//offsetX = Random.Range(0f, 9999f);
	//offsetY = Random.Range (0f, 9999f);
//}
// Use this for initialization
void Start () {
	offsetX = Random.Range(0f, 9999f);
	offsetY = Random.Range (0f, 9999f);
	Terrain terrain = GetComponent<Terrain>();
	terrain.terrainData = GenerateTerrain (terrain.terrainData);
}
TerrainData GenerateTerrain (TerrainData terrainData){
	terrainData.heightmapResolution = Width + 1;
	terrainData.size = new Vector3(Width, Depth, Height);
	terrainData.SetHeights (0, 0, GenerateHeights ());
	return terrainData;
}
float[,] GenerateHeights (){
	float[,] heights = new float[Width, Height];
	for (int x = 0; x < Width; x++) {
		for (int y = 0; y < Width; y++) {
			heights [x, y] = CalculateHeight (x,y);
		}
	}
	return heights;
}
float CalculateHeight (int x, int y){
	float xCoord = (float)x / Width * scale + offsetX;
	float yCoord = (float)y / Height * scale + offsetY;

	return Mathf.PerlinNoise (xCoord, yCoord);
}
// Update is called once per frame

}

However, when I use this script the player falls through the terrain or glitches about if they are not on flat ground.
I also got this script from this tutorial: GENERATING TERRAIN in Unity - Procedural Generation Tutorial - YouTube

Well since you change the terrain data after the terrain has been created you probably have to reassign the terrainData to the TerrainCollider. Though probably like this:

terrain.terrainData = GenerateTerrain (terrain.terrainData);
var collider = GetComponent<TerrainCollider>();
collider.terrainData = null;
collider.terrainData = terrain.terrainData;