Generate Random Terrain on Start

Making a game, need to generate random terrain on every start up.

Shamelessly stole someones code for terrain generation, it’s good but I can’t find where I got it from but all credit to that guy.

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

public class RandomTerrainGenerator : ScriptableWizard {
	
	//The higher the numbers, the more hills/mountains there are
	private float HM = Random.Range(0, 10);
	
	//The lower the numbers in the number range, the higher the hills/mountains will be...
	private float divRange = Random.Range(12, 25);
	
	[MenuItem("Terrain/Generate Random Terrain")]
	public static void CreateWizard(MenuCommand command)
	{
		ScriptableWizard.DisplayWizard("Generate Random Terrain", typeof(RandomTerrainGenerator));
	}
	
	void OnWizardCreate()
	{
		GameObject G = Selection.activeGameObject;
		if (G.GetComponent<Terrain>())
		{
			GenerateTerrain(G.GetComponent<Terrain>(), HM);
		}
	}
	
	//Our Generate Terrain function
	public void GenerateTerrain(Terrain t, float tileSize)
	{
		
		//Heights For Our Hills/Mountains
		float[,] hts = new float[t.terrainData.heightmapWidth, t.terrainData.heightmapHeight];
		for (int i = 0; i < t.terrainData.heightmapWidth; i++)
		{
			for (int k = 0; k < t.terrainData.heightmapHeight; k++)
			{
				hts[i, k] = Mathf.PerlinNoise(((float)i / (float)t.terrainData.heightmapWidth) * tileSize, ((float)k / (float)t.terrainData.heightmapHeight) * tileSize)/ divRange;
			}
		}
		Debug.LogWarning("DivRange: " + divRange + " , " + "HTiling: " + HM);
		t.terrainData.SetHeights(0, 0, hts);
	}
}

I’m using javascript primarily but what I want to do is use that script to generate terrain in an Awake function from another script. I can’t get unity to recognize the script i’m trying to get it from or something because I always get an error at the start no matter what I do.

var player : GameObject;
var terrainGenerator : MonoScript; //Someone told me monoscript, not really sure it's right though
var selfTerrain : Terrain;
private var terrainHeight : float;
private var playerx : float;
private var playerz : float;

function Awake () {

//Generate Terrain
selfTerrain.terrainData = terrainGenerator.GenerateTerrain();


}
function Start () {

//Set player at terrain height
playerx = player.transform.position.x;
playerz = player.transform.position.z;
terrainHeight = selfTerrain.terrainData.GetHeight(playerx, playerz);
player.transform.position.y = terrainHeight + 1;


}

Any help?

In order to call C# code from within Unityscript you need to put it in a folder that gets compiled earlier (see http://forum.unity3d.com/threads/mixing-c-and-javascript-how.23274/). For example, put the C# script in Assets/Plugins (create it if it doesn’t exist). That’s the main error you were getting. Then, the class of the C# code is what you would typically use. However, that won’t work properly in this case because the C# code you started with is intended to run in the editor, not at runtime.

Furthermore, multiple languages in your Unity codebase makes things really confusing, so here’s a port of the script you found to UnityScript. I haven’t used it for a few years (C# has more compile-time checking so I’ve been using it to write less buggy code), so I can’t claim that this code is clean, but it should be a good starting point and I’ve tested it by creating a terrain and adding the script to it.

var selfTerrain : Terrain; 

function Awake () {
	selfTerrain = GetComponent(Terrain);
	GenerateTerrain(100f);
}

function GenerateTerrain(tileSize: float) {

	//The higher the numbers, the more hills/mountains there are
	var HM : float = Random.Range (0, 10);
	//The lower the numbers in the number range, the higher the hills/mountains will be...
	var divRange:float  = Random.Range (12, 25);

	var height = selfTerrain.terrainData.heightmapHeight;
	var width = selfTerrain.terrainData.heightmapWidth;

	//Heights For Our Hills/Mountains
	var hts : float[,] = new float[width,height];
	for (var i :float = 0; i < width; i++) {
		for (var k :float = 0; k < height; k++) {
			hts [i, k] = Mathf.PerlinNoise ((i / width) * tileSize, (k / height) * tileSize) / divRange;
		}
	}
	Debug.LogWarning ("DivRange: " + divRange + " , " + "HTiling: " + HM);
	selfTerrain.terrainData.SetHeights (0, 0, hts);
}