- Home /
Problems with Translating Java in C#
I've been translating Inserting heights in TerrainData
everything that's in comment I don't know how to translate
Full Code
using UnityEngine;
using System.Collections;
public class Terraform : MonoBehaviour {
public Terrain terrain ;
public TerrainData tData;
public int xRes ;
public int yRes ;
// public float[,] heights; we don't need it as they are in functions thanks for noticing other than that how should I name it? (float)
public Terrain myTerrain ;
public int xBase = 0;
public int yBase = 0;
void Start () {
//terrain = transform.GetComponent(Terrain);
tData = terrain.terrainData;
xRes = tData.heightmapWidth;
yRes = tData.heightmapHeight;
//terrain.activeTerrain.heightmapMaximumLOD = 0;
}
void OnGUI() {
if(GUI.Button (new Rect (10, 10, 100, 25), "Wrinkle")) {
randomizePoints(0.1f);
}
if(GUI.Button (new Rect (10, 40, 100, 25), "Reset")) {
resetPoints();
}
}
void randomizePoints(float strength) {
var heights = tData.GetHeights(0, 0, xRes, yRes);
for (int y = 0; y < yRes; y++) {
for (int x = 0; x < xRes; x++) {
heights[x,y] = Random.Range(0.0f, strength) * 0.5f;
}
}
tData.SetHeights(0, 0, heights);
}
void resetPoints() {
var heights = tData.GetHeights(0, 0, xRes, yRes);
for (int y = 0; y < yRes; y++) {
for (int x = 0; x < xRes; x++) {
heights[x,y] = 0;
}
}
tData.SetHeights(0, 0, heights);
}
// here I'm trying to make my own version as basics - learning it
void OnMouseDown() {
var heights = tData.GetHeights(0, 0, xRes, yRes);
heights[5,4] = 20;
tData.SetHeights(0, 0, heights);
}
}
EDIT:
I changed the Question as they figured out array was declared in globally while using in local witch I didn't notice at all
trying to do similar version in OnMouseDown(){} but with more control
Answer by Doireth · Feb 02, 2013 at 08:21 AM
public float[] heights;
Should be
public float[,] heights;
To indicate on declaration that it is a 2-dimensional array
EDIT: In randomizePoints() you are declaring a local variable called heights also, which is also initialized and assigned values. In OnMouseDown() you are referencing the class variable heights which has not been initialized.
ok but why does it understand
heights[x,y] = 0;
for
public float heights[] ;
and if I do it as you said I get
NullReferenceException: Object reference not set to an instance of an object
Terraform.On$$anonymous$$ouseDown () (at Assets/Terraform.cs:64)
UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32)
for mouse down while I get non error for above code in same script WTH
ok and am strange error that points me am nowhere
!CompareApproximately (det, 1.0F, .005f)
UnityEditor.SceneView:.ctor()
As mentioned above you have many times heights of different types. If you have a global heights, try not to use local ones as they would hide the global one. Even though it would be intended, it makes it hard on others to read your code.
yeah thanks for noticing
I was translating code not writing 1 thank you very much I've finally managed to fully translate the javascript
I hate java