x


Script Error

I have this script which I think is supposed to set the height of my terrain so I can lower it but when I click on the option which is called raise or lower terrain I get an error saying "method not found UnityEngine.Object.GetComponent". Can anyone help me out with this?

class RaiseHeightmap extends ScriptableWizard {

var addHeight = .1;

static var terrain : TerrainData;



@MenuItem ("Terrain/Raise or Lower Heightmap...")

static function CreateWizard () {

    terrain = null;

    var terrainObject : Terrain = Selection.activeObject.GetComponent(Terrain) as Terrain;

    if (!terrainObject) {

        terrainObject = Terrain.activeTerrain;

    }

    if (terrainObject) {

        terrain = terrainObject.terrainData;

        var buttonText = "Apply Height";

    }

    else {

        buttonText = "Cancel";

    }

    ScriptableWizard.DisplayWizard("Raise/Lower Heightmap", RaiseHeightmap, buttonText);

}



function OnWizardUpdate () {

    if (!terrain) {

        helpString = "No terrain found";

        return;

    }



    addHeight = Mathf.Clamp(addHeight, -1.0, 1.0);

    helpString = (terrain.size.y*addHeight) + " meters (" + parseInt(addHeight*100.0) + "%)";

}



function OnWizardCreate () {

    if (!terrain) {

        return;

    }

    Undo.RegisterUndo(terrain, "Raise or Lower Heightmap");



    var heights = terrain.GetHeights(0, 0, terrain.heightmapWidth, terrain.heightmapHeight);

    for (y = 0; y < terrain.heightmapHeight; y++) {

        for (x = 0; x < terrain.heightmapWidth; x++) {

            heights[y,x] = heights[y,x] + addHeight;

        }

    }

    terrain.SetHeights(0, 0, heights);

    terrain = null;

}

}

more ▼

asked Mar 29 '12 at 06:54 PM

Dreave gravatar image

Dreave
137 82 95 96

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

GetComponent is a member of GameObject, and Selection.activeObject is simply an Object. You'll need to cast Selection.activeObject.

Related documentation:

Selection.activeObject / GetComponent

more ▼

answered Mar 29 '12 at 07:09 PM

nschrag gravatar image

nschrag
245 6 9 13

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5272
x3891
x1521

asked: Mar 29 '12 at 06:54 PM

Seen: 307 times

Last Updated: Mar 30 '12 at 06:59 PM