Quantcast
Channel: Questions in topic: "error"
Viewing all articles
Browse latest Browse all 7934

Having trouble getting the sample code for Mathf.PerlinNoise to work.

$
0
0
I'm referring to [this page][1], with the specific sample code as follows: ---------- using UnityEngine; using System.Collections; public class Example : MonoBehaviour { public int pixWidth; public int pixHeight; public float xOrg; public float yOrg; public float scale = 1.0F; private Texture2D noiseTex; private Color[] pix; void Start() { noiseTex = new Texture2D(pixWidth, pixHeight); pix = new Color[noiseTex.width * noiseTex.height]; renderer.material.mainTexture = noiseTex; } void CalcNoise() { float y = 0.0F; while (y < noiseTex.height) { float x = 0.0F; while (x < noiseTex.width) { float xCoord = xOrg + x / noiseTex.width * scale; float yCoord = yOrg + y / noiseTex.height * scale; float sample = Mathf.PerlinNoise(xCoord, yCoord); pix[y * noiseTex.width + x] = new Color(sample, sample, sample); x++; } y++; } noiseTex.SetPixels(pix); noiseTex.Apply(); } void Update() { CalcNoise(); } } ---------- As a pretty new beginner, I don't really know how to apply this, or what to make it a component of. That's part of the problem. Before I even try anything, however, when I paste this sample code into a new script and save it, the console error immediately spits an error at me: Assets/Scripts/Perlin.cs(25,37): error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?) So, inside the CalcNoise() function, I looked at line 25, where it appears it doesn't like the fact that "pix[y * noiseTex + x]" contains x and y as floats instead of integers. I'm not sure how to fix it properly, but I tried starting by declaring x and y as integers. This at least made the error disappear from the console. I then tried adding a new plane to the scene and making this script a component of it, but then when I ran it I got a new error: Array size must be at least width*height UnityEngine.Texture2D:SetPixels(Color[]) Perlin:CalcNoise() (at Assets/Scripts/Perlin.cs:30) Perlin:Update() (at Assets/Scripts/Perlin.cs:34) ...And from here, I'm shamefully lost. [1]: https://docs.unity3d.com/Documentation/ScriptReference/Mathf.PerlinNoise.html

Viewing all articles
Browse latest Browse all 7934

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>