- Home /
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!
Are you trying to copy the second block right after the first one ? If yes, you can't.
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.
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.
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?
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!
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!
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);
Your answer
Follow this Question
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