Real-time terrain editor?

Ok so I have this code:

using UnityEngine;
using System.Collections;

public class TerrainEditing : MonoBehaviour {
	private Ray ray;
	private RaycastHit hit;
	public Terrain terrain;
	
	void Update () {
		if (Input.GetMouseButtonDown(0))
		{
			ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			if (Physics.Raycast(ray, out hit, 100))
			{
			Vector3 pos = hit.point;
			terrain.terrainData.SetHeights((int)pos.x, (int)pos.y, new float[100,100]);
			}
		}
	}
}

But for some reason, this doesn’t work! I just want to click anywhere on the terrain and have it raise to what height I want.

Can anyone see something wrong with this code?

Thanks,

Alex

You’re trying to access terrain which hasn’t been initialized. What you should be doing instead is creating a new TerrainData, using the methods in that TerrainData to initialize the terrain settings, and then calling Terrain.CreateTerrainGameObject(the newly created TerrainData).

It’s easier to just instantiate a terrain prefab though. 0_0

What if I already have a terrain in game, though? I linked my public terrain variable to the active terrain in my scene.

Excuse my stupidity, I’m still trying to get used to Unity’s ‘coding’. :stuck_out_tongue: