Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Altering terrain in game using C#

Discussion in 'Scripting' started by YBOT, Jul 1, 2015.

  1. YBOT

    YBOT

    Joined:
    Feb 15, 2014
    Posts:
    2
    I am trying to create a basic C# script that allows me to alter a particular spot of terrain in game.

    This is what I've come up with so far,

    using UnityEngine;
    using System.Collections;

    public class ChangeTerrainHeight : MonoBehaviour {

    public Terrain TerrainMain;

    void OnGui()
    {

    if(GUI.Button(new Rect(20, 40, 200, 30), "Change Terrain Height"))
    {


    //get the terrain heightmap width and height.
    int xRes = TerrainMain.terrainData.heightmapWidth;
    int yRes = TerrainMain.terrainData.heightmapHeight;


    //GetHeights - gets the heightmap points of the terran. Store those values in a float array.
    float [,] heights = TerrainMain.terrainData.GetHeights(0, 0, xRes, yRes);


    //Manipulate the height data
    heights[10, 10] = 1f; //0 - 1. 1 being the maximum possible height



    //SetHeights to change the terrain height.
    TerrainMain.terrainData.SetHeights (0, 0, heights);


    }

    }

    }

    //This script is put on my terrain which is named TerrainMain

    All it is supposed to do is create a button and when I click on that button it is supposed to change the height of the terrain at 10, 10 to its limit. I am not even getting a button when I play.

    Thanks for any/all help
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    OnGui() should be OnGUI()
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    please use [code ][/code ] tags when you paste code into the forums, really helps with the readability, there is a sticky on them at the top of the scripting forum