Object Reference Error
I am trying to use a SimplexNoise.cs script along side a voxel terrain system that I have been making. Now it is giving an error when I try to get the noise via a return.
An object reference is required for the non-static field, method, or property 'SimplexNoise.Generate(double, double, double)'
The method I am referencing is here:
// Generates 3D simplex noise
public double Generate(double x, double y, double z)
{
// Skew the input space to determine which simplex cell we're in
double s = (x + y + z) * .33; // Very nice and simple skew factor for 3D
int i = FastFloor(x + s);
int j = FastFloor(y + s);
int k = FastFloor(z + s);
double t = (i + j + k) * (1.0 / 6.0);
double _x0 = i - t; // Unskew the cell origin back to (x,y,z) space
double _y0 = j - t;
double _z0 = k - t;
double x0 = x - _x0; // The x,y,z distances from the cell origin
double y0 = y - _y0;
double z0 = z - _z0;
// For the 3D case, the simplex shape is a slightly irregular tetrahedron.
// Determine which simplex we are in.
int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
if (x0 >= y0)
{
if (y0 >= z0) // X Y Z order
{
i1 = 1; j1 = 0; k1 = 0;
i2 = 1; j2 = 1; k2 = 0;
}
else if (x0 >= z0) // X Z Y order
{
i1 = 1; j1 = 0; k1 = 0;
i2 = 1; j2 = 0; k2 = 1;
}
else // Z X Y order
{
i1 = 0; j1 = 0; k1 = 1;
i2 = 1; j2 = 0; k2 = 1;
}
}
else // x0<y0
{
if (y0 < z0) // Z Y X order
{
i1 = 0; j1 = 0; k1 = 1;
i2 = 0; j2 = 1; k2 = 1;
}
else if (x0 < z0) // Y Z X order
{
i1 = 0; j1 = 1; k1 = 0;
i2 = 0; j2 = 1; k2 = 1;
}
else // Y X Z order
{
i1 = 0; j1 = 1; k1 = 0;
i2 = 1; j2 = 1; k2 = 0;
}
}
// A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
// a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
// a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
// c = 1/6.
double x1 = x0 - i1 + (1.0 / 6.0); // Offsets for second corner in (x,y,z) coords
double y1 = y0 - j1 + (1.0 / 6.0);
double z1 = z0 - k1 + (1.0 / 6.0);
double x2 = x0 - i2 + 2.0 * (1.0 / 6.0); // Offsets for third corner in (x,y,z) coords
double y2 = y0 - j2 + 2.0 * (1.0 / 6.0);
double z2 = z0 - k2 + 2.0 * (1.0 / 6.0);
double x3 = x0 - 1.0 + 3.0 * (1.0 / 6.0); // Offsets for last corner in (x,y,z) coords
double y3 = y0 - 1.0 + 3.0 * (1.0 / 6.0);
double z3 = z0 - 1.0 + 3.0 * (1.0 / 6.0);
// Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
int ii = i & 255;
int jj = j & 255;
int kk = k & 255;
int gi0 = permMod12[ii + perm[jj + perm[kk]]];
int gi1 = permMod12[ii + i1 + perm[jj + j1 + perm[kk + k1]]];
int gi2 = permMod12[ii + i2 + perm[jj + j2 + perm[kk + k2]]];
int gi3 = permMod12[ii + 1 + perm[jj + 1 + perm[kk + 1]]];
double n0, n1, n2, n3;
// Calculate the contribution from the four corners
double t0 = .6 - x0 * x0 - y0 * y0 - z0 * z0;
if (t0 < 0) n0 = 0.0;
else
{
t0 *= t0;
n0 = t0 * t0 * gradDot(grad3[gi0], x0, y0, z0);
}
double t1 = .6 - x1 * x1 - y1 * y1 - z1 * z1;
if (t1 < 0) n1 = 0.0;
else
{
t1 *= t1;
n1 = t1 * t1 * gradDot(grad3[gi1], x1, y1, z1);
}
double t2 = .6 - x2 * x2 - y2 * y2 - z2 * z2;
if (t2 < 0) n2 = 0.0;
else
{
t2 *= t2;
n2 = t2 * t2 * gradDot(grad3[gi2], x2, y2, z2);
}
double t3 = .6 - x3 * x3 - y3 * y3 - z3 * z3;
if (t3 < 0) n3 = 0.0;
else
{
t3 *= t3;
n3 = t3 * t3 * gradDot(grad3[gi3], x3, y3, z3);
}
// Add contributions from each corner to get the final noise value.
// The result is scaled to stay just inside [-1,1]
return 32.0 * (n0 + n1 + n2 + n3); // TODO: The scale factor is preliminary!
}
And the method that contains the error is here:
public static int GetNoise(int x, int y, int z, float scale, int max)
{
return Mathf.FloorToInt((SimplexNoise.Generate(x * scale, y * scale, z * scale) + 1f) * (max / 2f));
}
I have attempted to remove the static tag from the int GetNoise method with no change, the method referenced is spelled the same in both scripts, the namespace for both is spelled SimxNoise. And I have dug through as many questions on here as I could to try and find some sort of answer to no avail.
Any help or a point towards the right direction to look for an answer would be greatly appreciated.
Answer by Darkhanis · Apr 16, 2017 at 01:18 AM
This is actually a problem of program syntax rather than an issue with unity or whatsoever. The problem here is that you are trying to call an Instance Method like if it was a Class Method.
You can solve this by doing one of these two things:
Change the Generate method on the SimplexNoise class to static (this might rise some other problems if its using instance variables inside this method).
Get an instance of SimplexNoise so you can say "simplexNoiseInstance.Generate...)
Let me know if you need further assistance :).
I was able to fix my issue by changing the SimplexNoise method name to Noise rather than SimplexNoise, as well as changing a wide range of specific methods within the SimplexNoise script to Static. Thank you for you answer. I had already finished doing this upon realizing the issue but hopefully this will help others that are experiencing the same sort of problem.
Answer by LovelyVibes · Apr 16, 2017 at 01:26 AM
I fixed the error by setting a static double called noise at the top of the Terrain gen class
static double noise;
Then I used the System function Math for dealing with the double, then returned it using the System function Covert.ToInt64 (in this case you can use Convert.ToInt32 if working with a normal int) with the value of noise as shown below.
public static long GetNoise(int x, int y, int z, double scale, int max)
{
//Converts the double Generate to a long referenced through noise
noise = Math.Ceiling((Noise.Generate(x * scale, y * scale, z * scale) + 1.0) *
(max / 2.0));
return Convert.ToInt64(noise);
}
I also changed the class name from SimplexNoise to Noise
public class Noise
Using the Mathf function did not work in this case as I was trying to convert the double from the SimplexNoise script to an int, and using Mathf unbeknownst to me prior to this only works when using a float. So I had to use the System built-in function of Math.
Here are the documentations for both the unity Mathf function, and the System built-in Math function.
Mathf: https://docs.unity3d.com/ScriptReference/Mathf.html
Math: https://msdn.microsoft.com/en-us/library/system.math(v=vs.110).aspx
I hope this helps anyone that was having the same issue as me when trying to use SimplexNoise with their own procedural systems.
EDIT: I forgot to mention that I also had to switch some methods within the SimplexNoise script to static along with the Generate method I was calling.
I have posted the edited version of the script on this forum thread if you are wanting to discuss the script or any issues you have there I will try my best to assist.
https://forum.unity3d.com/threads/simplexnoise-script-c.466469/
Your answer
![](https://koobas.hobune.stream/wayback/20220612115919im_/https://answers.unity.com/themes/thub/images/avi.jpg)