Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sdgd · Feb 02, 2013 at 08:10 AM · c#terraindatatranslationsetheights

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sdgd · Feb 02, 2013 at 08:45 AM 0
Share

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()
avatar image Doireth · Feb 02, 2013 at 08:52 AM 0
Share

See my edited answer.

avatar image oulaoup · Feb 02, 2013 at 09:02 AM 0
Share

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.

avatar image sdgd · Feb 02, 2013 at 09:23 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I programmatically stretch a Terrain? 1 Answer

terraindata.setheight as a brush for runtime terrain editing 1 Answer

Can be terrainData.setHeights' values negative? 1 Answer

Getting a GameObjects position in relation to a Terrains Resolution 1 Answer

Issue copying heightmaps from a LARGE terrain to a new, small, cutout Terrain with GETHEIGHTS and SETHEIGHTS 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges