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 WorldWideGlide · Nov 06, 2013 at 02:04 AM · getcomponent

Running a script function using "GetComponent"

I am trying to automatically run a Heightmapping (terrain generator) script using 'GetComponent'......

My code looks like this:

 if(Input.GetKeyDown (KeyCode.G))
             {
                 TerrainData Tdata = new TerrainData();
                             GameObject T1 = Terrain.CreateTerrainGameObject(Tdata);
     T1.AddComponent("TerrainToolkit");

This works fine but now I need to access the TerrainToolkit and run the code that gives the terrain its heightmapping, I found this section which I believe is what I need to run:

 public float[,] generateDiamondSquare(float[,] heightMap, Vector2 arraySize, GeneratorProgressDelegate generatorProgressDelegate) {
         int Tw = (int) arraySize.x;
         int Th = (int) arraySize.y;
         float heightRange = 1.0f;
         int step = Tw - 1;
         heightMap[0, 0] = 0.5f;
         heightMap[Tw - 1, 0] = 0.5f;
         heightMap[0, Th - 1] = 0.5f;
         heightMap[Tw - 1, Th - 1] = 0.5f;
         generatorProgressDelegate("Fractal Generator", "Generating height map. Please wait.", 0.0f);
         while (step > 1) {
             // diamond
             for (int Tx = 0; Tx < Tw - 1; Tx += step){
                 for (int Ty = 0; Ty < Th - 1; Ty += step){
                     int sx = Tx + (step >> 1);
                     int sy = Ty + (step >> 1);
                     Vector2[] points = new Vector2[4];
                     points[0] = new Vector2(Tx, Ty);
                     points[1] = new Vector2(Tx + step, Ty);
                     points[2] = new Vector2(Tx, Ty + step);
                     points[3] = new Vector2(Tx + step, Ty + step);
                     dsCalculateHeight(heightMap, arraySize, sx, sy, points, heightRange);
                 }
             }
             // square
             for (int Tx = 0; Tx < Tw - 1; Tx += step) {
                 for (int Ty = 0; Ty < Th - 1; Ty += step) {
                     int halfstep = step >> 1;
                     int x1 = Tx + halfstep;
                     int y1 = Ty;
                     int x2 = Tx;
                     int y2 = Ty + halfstep;
                     Vector2[] points1 = new Vector2[4];
                     points1[0] = new Vector2(x1 - halfstep, y1);
                     points1[1] = new Vector2(x1, y1 - halfstep);
                     points1[2] = new Vector2(x1 + halfstep, y1);
                     points1[3] = new Vector2(x1, y1 + halfstep);
                     Vector2[] points2 = new Vector2[4];
                     points2[0] = new Vector2(x2 - halfstep, y2);
                     points2[1] = new Vector2(x2, y2 - halfstep);
                     points2[2] = new Vector2(x2 + halfstep, y2);
                     points2[3] = new Vector2(x2, y2 + halfstep);
                     dsCalculateHeight(heightMap, arraySize, x1, y1, points1, heightRange);
                     dsCalculateHeight(heightMap, arraySize, x2, y2, points2, heightRange);
                 }
             }
             heightRange *= diamondSquareDelta;
             step >>= 1;
         }
         generatorProgressDelegate("Fractal Generator", "Generating height map. Please wait.", 1.0f);
         return heightMap;
     }

I then use the command T1.GetComponent(TerrainToolkit)().insert command here; I have been able to successfully change variables within terraintoolkit, but when I insert the above lines of code it gives me an error saying "}" expected right before "public float"

Is my approach correct? This is very confusing and I apologize for the wall of text!

Comment
Add comment · Show 7
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 julienfouilhe · Nov 06, 2013 at 02:15 AM 0
Share

Are you trying to copy the second block right after the first one ? If yes, you can't.

avatar image Huacanacha · Nov 06, 2013 at 02:39 AM 0
Share

Do you mean you want to call the generateDiamondSquare method on the TerrainToolkit script? Try using the generic GetComponent method. I don't have time to test it right now but I think something like this should work (apologies if I get this wrong!):

 T1.GetComponent<TerrainToolkit>(). generateDiamondSquare(...);

Obviously you'll need to fill in your parameters where the '...' are.

If that doesn't work post the exact code you're trying to run.

avatar image Starwalker · Nov 06, 2013 at 02:50 AM 0
Share

The "if statement" parenthesis is not closed at the end of the file, the last parenthesis after "return height-map" is from the public float function '{', add a "}" before the public float or after the last parenthesis after return height-map. I hope this is the solution, if they are in the same script.

avatar image WorldWideGlide · Nov 07, 2013 at 12:44 AM 0
Share

@Huacanacha

Yes that is what I am trying to do,

$$anonymous$$y script looks like this:

 public class GenerateTerrain : $$anonymous$$onoBehaviour 
 {
     void Update ()
     {    
         
         if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.G))
         {
             TerrainData Tdata = new TerrainData();
        GameObject T1 = Terrain.CreateTerrainGameObject(Tdata);
                             T1.AddComponent("TerrainToolkit");
                T1.GetComponent<TerrainToolkit>().SO$$anonymous$$E CO$$anonymous$$$$anonymous$$AND;

This works fine for generating the terrain, adding the toolkit and I can modify variables within the toolkit. But when it comes to running a function I am a bit lost.....

![alt text][1] [1]: /storage/temp/17524-untitled.png

 Essentially I want to press this button here using a script... The terrainToolkit is a script component attached to the terrain T1 and is associated with an editor file as well.. I assume this is what defines the keys?
untitled.png (58.6 kB)
avatar image Huacanacha WorldWideGlide · Nov 07, 2013 at 12:54 AM 0
Share

You should just be able to call the function directly, like this:

 T1.GetComponent<TerrainToolkit>().generateDiamondSquare(...);

With whatever parameters you want to call it with in place of the "...". Does this not work?

Also, please don't use Answers as comments... it makes the post confusing for everyone!

avatar image WorldWideGlide · Nov 07, 2013 at 02:11 AM 0
Share

Ok yes it seems to be recognizing this command

 `T1.GetComponent<TerrainToolkit>().generateDiamondSquare(heightRange = 1.0f, height$$anonymous$$ap, points[0] = new Vector2(Tx, Ty), GeneratorProgressDelegate generatorProgressDelegate);`

I filled in the parameters as I thought appropriate

But I am getting an error at the space between GeneratorProgressDelegate and generatorProgressDelegate it says ")" is expected. That cant be right, perhaps I filled in the fields incorrectly? I think it is very close to working!

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by WorldWideGlide · Nov 10, 2013 at 08:03 PM

OK I solved it, What I had to do was select the parameters on the editor menus and run it

 T1.AddComponent("TerrainToolkit");
     T1.GetComponent<TerrainToolkit>().generatorTypeInt = 1;        
     T1.GetComponent<TerrainToolkit>().diamondSquareDelta = 0.4f;
     T1.GetComponent<TerrainToolkit>().diamondSquareBlend = 1.0f;
     T1.GetComponent<TerrainToolkit>().generateTerrain(GeneratorProgressDelegategeneratorProgressDelegate);
Comment
Add comment · 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

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

17 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Any problems with this script? 1 Answer

Error accessing var from other scripts within project via GetComponent 1 Answer

How do I select a joint component from a GameObject that has multiple joint components? 1 Answer

Implementing Setter/Getter 0 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