I am trying to generate Noise on iPhone, works great in editor, in XCode I get a debug error for this part of the script.

class NoiseArray3D {

private const int step = 4;

private PerlinNoise3D noise;
private float[,] map = new float[Chunk.SIZE_X+2, Chunk.SIZE_Y+2, Chunk.SIZE_Z+2];
private Vector3i offset;

public NoiseArray3D(float scale) {
noise = new PerlinNoise3D(scale);
}

public void GenerateNoise(int offsetX, int offsetY, int offsetZ) {
GenerateNoise( new Vector3i(offsetX, offsetY, offsetZ) );
}

public void GenerateNoise(Vector3i offset) {
this.offset = offset;

int sizeX = Chunk.SIZE_X + 2;
int sizeY = Chunk.SIZE_Y + 2;
int sizeZ = Chunk.SIZE_Z + 2;

for(int x=0; x<sizeX; x+=step) {
for(int y=0; y<sizeY; y+=step) {
for(int z=0; z<sizeZ; z+=step) {
Vector3i a = new Vector3i(x, y, z) + offset;
Vector3i b = a + new Vector3i(step, step, step);

float a1 = noise.Noise(a.x, a.y, a.z);
float a2 = noise.Noise(b.x, a.y, a.z);
float a3 = noise.Noise(a.x, b.y, a.z);
float a4 = noise.Noise(b.x, b.y, a.z);

float b1 = noise.Noise(a.x, a.y, b.z);
float b2 = noise.Noise(b.x, a.y, b.z);
float b3 = noise.Noise(a.x, b.y, b.z);
float b4 = noise.Noise(b.x, b.y, b.z);

for(int tx=0; tx<step x+tx<sizeX; tx++) {
for(int ty=0; ty<step y+ty<sizeY; ty++) {
for(int tz=0; tz<step z+tz<sizeZ; tz++) {
float fx = (float) tx/step;
float fy = (float) ty/step;
float fz = (float) tz/step;
float ta1 = Mathf.Lerp(a1, a2, fx);
float ta2 = Mathf.Lerp(a3, a4, fx);
float ta3 = Mathf.Lerp(ta1, ta2, fy);

float tb1 = Mathf.Lerp(b1, b2, fx);
float tb2 = Mathf.Lerp(b3, b4, fx);
float tb3 = Mathf.Lerp(tb1, tb2, fy);

float val = Mathf.Lerp(ta3, tb3, fz);
map[x+tx, y+ty, z+tz] = val;
}
}
}
}
}
}
}

public float GetNoise(Vector3i pos) {
return GetNoise(pos.x, pos.y, pos.z);
}

public float GetNoise(int x, int y, int z) {
x -= offset.x;
y -= offset.y;
z -= offset.z;
return map[x+1, y+1, z+1];
}

}

Could you also tell us what kind of debug error you get?

When I run in Xcode, With my iPhone 5 connected I get this : 0x41d00: vstr s0, [r0]

this _NoiseArray3D 0x02e9df20
noise _PerlinNoise3D 0x02ea2f78
perm void * 0x02eaedb0
scale float 0.0333333
map void * 0x02ea7000
offset int 0

And all this:

2013-03-04 21:42:39.310 hungercraft[6318:907] → registered mono modules 0x12bdf00
→ applicationDidFinishLaunching()
Mono path[0] = ‘/var/mobile/Applications/36F425D9-935A-400E-BE95-730CF21F02C0/hungercraft.app/Data/Managed’
PlayerConnection initialized from /var/mobile/Applications/36F425D9-935A-400E-BE95-730CF21F02C0/hungercraft.app/Data (debug = 0)
PlayerConnection initialized network socket : 0.0.0.0 55269
Multi-casting “[IP] 10.215.142.8 [Port] 55269 [Flags] 3 [Guid] 1458173381 [EditorId] 360466893 [Version] 1048832 [Id] iPhonePlayer(Jordans-iPhone) [Debug] 1” to [225.0.0.222:54997]…
Waiting for connection from host on [10.215.142.8:55269]…
Timed out. Continuing without host connection.
PlayerConnection already initialized - listening to [10.215.142.8:55269]
Renderer: PowerVR SGX 543
Vendor: Imagination Technologies
Version: OpenGL ES 2.0 IMGSGX543-73.16.1
GL_OES_depth_texture GL_OES_depth24 GL_OES_element_index_uint GL_OES_fbo_render_mipmap GL_OES_mapbuffer GL_OES_packed_depth_stencil GL_OES_rgb8_rgba8 GL_OES_standard_derivatives GL_OES_texture_float GL_OES_texture_half_float GL_OES_texture_half_float_linear GL_OES_vertex_array_object GL_EXT_blend_minmax GL_EXT_color_buffer_half_float GL_EXT_debug_label GL_EXT_debug_marker GL_EXT_discard_framebuffer GL_EXT_map_buffer_range GL_EXT_occlusion_query_boolean GL_EXT_read_format_bgra GL_EXT_separate_shader_objects GL_EXT_shader_framebuffer_fetch GL_EXT_shader_texture_lod GL_EXT_shadow_samplers GL_EXT_texture_filter_anisotropic GL_EXT_texture_rg GL_EXT_texture_storage GL_APPLE_copy_texture_levels GL_APPLE_framebuffer_multisample GL_APPLE_rgb_422 GL_APPLE_sync GL_APPLE_texture_format_BGRA8888 GL_APPLE_texture_max_level GL_IMG_read_format GL_IMG_texture_compression_pvrtc
Creating OpenGLES2.0 graphics device
Initialize engine version: 4.0.1f2
Platform assembly: /var/mobile/Applications/36F425D9-935A-400E-BE95-730CF21F02C0/hungercraft.app/Data/Managed/UnityEngine.dll (this message is harmless)
Platform assembly: /var/mobile/Applications/36F425D9-935A-400E-BE95-730CF21F02C0/hungercraft.app/Data/Managed/Assembly-CSharp.dll (this message is harmless)
Platform assembly: /var/mobile/Applications/36F425D9-935A-400E-BE95-730CF21F02C0/hungercraft.app/Data/Managed/Assembly-UnityScript.dll (this message is harmless)
Non platform assembly: /private/var/mobile/Applications/36F425D9-935A-400E-BE95-730CF21F02C0/hungercraft.app/Data/Managed/Boo.Lang.dll (this message is harmless)
Non platform assembly: /private/var/mobile/Applications/36F425D9-935A-400E-BE95-730CF21F02C0/hungercraft.app/Data/Managed/System.dll (this message is harmless)
Non platform assembly: /private/var/mobile/Applications/36F425D9-935A-400E-BE95-730CF21F02C0/hungercraft.app/Data/Managed/Mono.Security.dll (this message is harmless)

• Completed reload, in 0.094 seconds
→ applicationDidBecomeActive()
NullReferenceException: A null value was found where an object instance was required.
at GameMech.Update () [0x00027] in /Users/tristantotty/HungerCraft/Assets/GameMech.js:21

(Filename: /Users/tristantotty/HungerCraft/Assets/GameMech.js Line: 21)

Did you already check that out?

Yes still same error

And there aren’t any more NullReferenceException messages in log?

Which Xcode version you using,?

No more NullReferences, and I am using the very latest xcode : 4.6
And by the way, when I build and run on my phone, the ground does not generate, which relys on the Perlin Noise Script.

Is the execution order of the scripts same with editor and phone? Does the Perlin Noise script include some platform macros?

How do i check the execution order? and the script in the OP is the Perlin Noise Script.

I meant the code of PerlinNoise3D.

You can control script execution order from Edit → Project settings → Script Execution Order, and you can use Debug.Log to print out the order of Start() functions (in case you init scripts from different scripts).

Here we go!:

using UnityEngine;
using System.Collections;

public class PerlinNoise3D {

private const int GradientSizeTable = 256;
private Vector3[ ] gradients = new Vector3[GradientSizeTable];

private short[ ] perm = new short[ ] {
225, 155, 210, 108, 175, 199, 221, 144, 203, 116, 70, 213, 69, 158, 33, 252,
5, 82, 173, 133, 222, 139, 174, 27, 9, 71, 90, 246, 75, 130, 91, 191,
169, 138, 2, 151, 194, 235, 81, 7, 25, 113, 228, 159, 205, 253, 134, 142,
248, 65, 224, 217, 22, 121, 229, 63, 89, 103, 96, 104, 156, 17, 201, 129,
36, 8, 165, 110, 237, 117, 231, 56, 132, 211, 152, 20, 181, 111, 239, 218,
170, 163, 51, 172, 157, 47, 80, 212, 176, 250, 87, 49, 99, 242, 136, 189,
162, 115, 44, 43, 124, 94, 150, 16, 141, 247, 32, 10, 198, 223, 255, 72,
53, 131, 84, 57, 220, 197, 58, 50, 208, 11, 241, 28, 3, 192, 62, 202,
18, 215, 153, 24, 76, 41, 15, 179, 39, 46, 55, 6, 128, 167, 23, 188,
106, 34, 187, 140, 164, 73, 112, 182, 244, 195, 227, 13, 35, 77, 196, 185,
26, 200, 226, 119, 31, 123, 168, 125, 249, 68, 183, 230, 177, 135, 160, 180,
12, 1, 243, 148, 102, 166, 38, 238, 251, 37, 240, 126, 64, 74, 161, 40,
184, 149, 171, 178, 101, 66, 29, 59, 146, 61, 254, 107, 42, 86, 154, 4,
236, 232, 120, 21, 233, 209, 45, 98, 193, 114, 78, 19, 206, 14, 118, 127,
48, 79, 147, 85, 30, 207, 219, 54, 88, 234, 190, 122, 95, 67, 143, 109,
137, 214, 145, 93, 92, 100, 245, 0, 216, 186, 60, 83, 105, 97, 204, 52
};

private float scale = 1;

public PerlinNoise3D(float scale) {
this.scale = scale;
for (int i = 0; i < GradientSizeTable; i++) {
float z = 1f - 2f * Random.value;
float r = Mathf.Sqrt(1 - z * z);
float theta = 2 * Mathf.PI * Random.value;
gradients_.x = r * Mathf.Cos(theta);_
gradients_.y = r * Mathf.Sin(theta);
}
}_

* public float Noise(float x, float y, float z) {*
_ return PerlinNoise(xscale, yscale, zscale) + 0.5f;
}
private float PerlinNoise(float x, float y, float z) {
int ix = (int) Mathf.Floor(x);
float fx0 = x - ix;
float fx1 = fx0 - 1;
float wx = Smooth(fx0);
int iy = (int) Mathf.Floor(y);
float fy0 = y - iy;
float fy1 = fy0 - 1;
float wy = Smooth(fy0);
int iz = (int) Mathf.Floor(z);
float fz0 = z - iz;
float fz1 = fz0 - 1;
float wz = Smooth(fz0);
float vx0 = Lattice(ix, iy, iz, fx0, fy0, fz0);
float vx1 = Lattice(ix + 1, iy, iz, fx1, fy0, fz0);
float vy0 = Mathf.Lerp(vx0, vx1, wx);
vx0 = Lattice(ix, iy + 1, iz, fx0, fy1, fz0);
vx1 = Lattice(ix + 1, iy + 1, iz, fx1, fy1, fz0);
float vy1 = Mathf.Lerp(vx0, vx1, wx);
float vz0 = Mathf.Lerp(vy0, vy1, wy);
vx0 = Lattice(ix, iy, iz + 1, fx0, fy0, fz1);
vx1 = Lattice(ix + 1, iy, iz + 1, fx1, fy0, fz1);
vy0 = Mathf.Lerp(vx0, vx1, wx);
vx0 = Lattice(ix, iy + 1, iz + 1, fx0, fy1, fz1);
vx1 = Lattice(ix + 1, iy + 1, iz + 1, fx1, fy1, fz1);
vy1 = Mathf.Lerp(vx0, vx1, wx);
float vz1 = Mathf.Lerp(vy0, vy1, wy);
return Mathf.Lerp(vz0, vz1, wz);
}*_

private int Index(int ix, int iy, int iz) {
// Turn an XYZ triplet into a single gradient table index.
return Permutate(ix + Permutate(iy + Permutate(iz)));
}

* private int Permutate(int x) {*
}
private float Lattice(int ix, int iy, int iz, float fx, float fy, float fz) {
// Look up a random gradient at [ix,iy,iz] and dot it with the [fx,fy,fz] vector.
int index = Index(ix, iy, iz);
return gradients[index].x * fx +
_ gradients[index].y * fy +
}
private static float Smooth(float x) {
return x * x * (3 - 2 * x);
}_

}
class NoiseArray3D {

* private const int step = 4;*

* private PerlinNoise3D noise;*
* private float[,] map = new float[Chunk.SIZE_X+2, Chunk.SIZE_Y+2, Chunk.SIZE_Z+2];
_ private Vector3i offset;*_

* public NoiseArray3D(float scale) {*
* noise = new PerlinNoise3D(scale);*
* }*

* public void GenerateNoise(int offsetX, int offsetY, int offsetZ) {*
* GenerateNoise( new Vector3i(offsetX, offsetY, offsetZ) );*
* }*

* public void GenerateNoise(Vector3i offset) {*
* this.offset = offset;*

* int sizeX = Chunk.SIZE_X + 2;
int sizeY = Chunk.SIZE_Y + 2;
int sizeZ = Chunk.SIZE_Z + 2;*

for(int x=0; x<sizeX; x+=step) {
for(int y=0; y<sizeY; y+=step) {
* for(int z=0; z<sizeZ; z+=step) {*
* Vector3i a = new Vector3i(x, y, z) + offset;*
* Vector3i b = a + new Vector3i(step, step, step);*

* float a1 = noise.Noise(a.x, a.y, a.z);*
* float a2 = noise.Noise(b.x, a.y, a.z);*
* float a3 = noise.Noise(a.x, b.y, a.z);*
* float a4 = noise.Noise(b.x, b.y, a.z);*

* float b1 = noise.Noise(a.x, a.y, b.z);*
* float b2 = noise.Noise(b.x, a.y, b.z);*
* float b3 = noise.Noise(a.x, b.y, b.z);*
* float b4 = noise.Noise(b.x, b.y, b.z);*

* for(int tx=0; tx<step x+tx<sizeX; tx++) {*
* for(int ty=0; ty<step y+ty<sizeY; ty++) {*
* for(int tz=0; tz<step z+tz<sizeZ; tz++) {*
* float fx = (float) tx/step;*
* float fy = (float) ty/step;*
* float fz = (float) tz/step;*
* float ta1 = Mathf.Lerp(a1, a2, fx);*
* float ta2 = Mathf.Lerp(a3, a4, fx);*
* float ta3 = Mathf.Lerp(ta1, ta2, fy);*

* float tb1 = Mathf.Lerp(b1, b2, fx);*
* float tb2 = Mathf.Lerp(b3, b4, fx);*
* float tb3 = Mathf.Lerp(tb1, tb2, fy);*

* float val = Mathf.Lerp(ta3, tb3, fz);*
* map[x+tx, y+ty, z+tz] = val;*
* }*
* }*
* }*
* }*
}
}
}

* public float GetNoise(Vector3i pos) {*
* return GetNoise(pos.x, pos.y, pos.z);*
* }*

* public float GetNoise(int x, int y, int z) {*
* x -= offset.x;*
* y -= offset.y;*
* z -= offset.z;*
* return map[x+1, y+1, z+1];*
* }*

}

I had to modify the code since your paste doesn’t include everything, but no crash in here

``````using UnityEngine;
using System.Collections;

public class Vector3i{
public int x,y,z;

public Vector3i(int x,int y,int z){
this.x=x;
this.y=y;
this.z=z;
}

public static Vector3i operator +(Vector3i left, Vector3i right)
{
return new Vector3i(left.x + right.x, left.y + right.y, left.z + right.z);
}

}

public class PerlinNoise3D
{

private const int GradientSizeTable = 256;
private short[] perm = new short[] {
225, 155, 210, 108, 175, 199, 221, 144, 203, 116, 70, 213, 69, 158, 33, 252,
5, 82, 173, 133, 222, 139, 174, 27, 9, 71, 90, 246, 75, 130, 91, 191,
169, 138, 2, 151, 194, 235, 81, 7, 25, 113, 228, 159, 205, 253, 134, 142,
248, 65, 224, 217, 22, 121, 229, 63, 89, 103, 96, 104, 156, 17, 201, 129,
36, 8, 165, 110, 237, 117, 231, 56, 132, 211, 152, 20, 181, 111, 239, 218,
170, 163, 51, 172, 157, 47, 80, 212, 176, 250, 87, 49, 99, 242, 136, 189,
162, 115, 44, 43, 124, 94, 150, 16, 141, 247, 32, 10, 198, 223, 255, 72,
53, 131, 84, 57, 220, 197, 58, 50, 208, 11, 241, 28, 3, 192, 62, 202,
18, 215, 153, 24, 76, 41, 15, 179, 39, 46, 55, 6, 128, 167, 23, 188,
106, 34, 187, 140, 164, 73, 112, 182, 244, 195, 227, 13, 35, 77, 196, 185,
26, 200, 226, 119, 31, 123, 168, 125, 249, 68, 183, 230, 177, 135, 160, 180,
12, 1, 243, 148, 102, 166, 38, 238, 251, 37, 240, 126, 64, 74, 161, 40,
184, 149, 171, 178, 101, 66, 29, 59, 146, 61, 254, 107, 42, 86, 154, 4,
236, 232, 120, 21, 233, 209, 45, 98, 193, 114, 78, 19, 206, 14, 118, 127,
48, 79, 147, 85, 30, 207, 219, 54, 88, 234, 190, 122, 95, 67, 143, 109,
137, 214, 145, 93, 92, 100, 245, 0, 216, 186, 60, 83, 105, 97, 204, 52
};
private float scale = 1;

public PerlinNoise3D (float scale)
{
this.scale = scale;
for (int i = 0; i < GradientSizeTable; i++) {
float z = 1f - 2f * Random.value;
float r = Mathf.Sqrt (1 - z * z);
float theta = 2 * Mathf.PI * Random.value;
gradients [i].x = r * Mathf.Cos (theta);
gradients [i].y = r * Mathf.Sin (theta);
gradients [i].z = z;
}
}

public float Noise (float x, float y, float z)
{
return PerlinNoise (x * scale, y * scale, z * scale) + 0.5f;
}

private float PerlinNoise (float x, float y, float z)
{
int ix = (int)Mathf.Floor (x);
float fx0 = x - ix;
float fx1 = fx0 - 1;
float wx = Smooth (fx0);

int iy = (int)Mathf.Floor (y);
float fy0 = y - iy;
float fy1 = fy0 - 1;
float wy = Smooth (fy0);

int iz = (int)Mathf.Floor (z);
float fz0 = z - iz;
float fz1 = fz0 - 1;
float wz = Smooth (fz0);

float vx0 = Lattice (ix, iy, iz, fx0, fy0, fz0);
float vx1 = Lattice (ix + 1, iy, iz, fx1, fy0, fz0);
float vy0 = Mathf.Lerp (vx0, vx1, wx);

vx0 = Lattice (ix, iy + 1, iz, fx0, fy1, fz0);
vx1 = Lattice (ix + 1, iy + 1, iz, fx1, fy1, fz0);
float vy1 = Mathf.Lerp (vx0, vx1, wx);

float vz0 = Mathf.Lerp (vy0, vy1, wy);

vx0 = Lattice (ix, iy, iz + 1, fx0, fy0, fz1);
vx1 = Lattice (ix + 1, iy, iz + 1, fx1, fy0, fz1);
vy0 = Mathf.Lerp (vx0, vx1, wx);

vx0 = Lattice (ix, iy + 1, iz + 1, fx0, fy1, fz1);
vx1 = Lattice (ix + 1, iy + 1, iz + 1, fx1, fy1, fz1);
vy1 = Mathf.Lerp (vx0, vx1, wx);

float vz1 = Mathf.Lerp (vy0, vy1, wy);
return Mathf.Lerp (vz0, vz1, wz);
}

private int Index (int ix, int iy, int iz)
{
// Turn an XYZ triplet into a single gradient table index.
return Permutate (ix + Permutate (iy + Permutate (iz)));
}

private int Permutate (int x)
{
return perm [x  mask];
}

private float Lattice (int ix, int iy, int iz, float fx, float fy, float fz)
{
// Look up a random gradient at [ix,iy,iz] and dot it with the [fx,fy,fz] vector.
int index = Index (ix, iy, iz);
return gradients [index].x * fx +
gradients [index].y * fy +
gradients [index].z * fz;
}

private static float Smooth (float x)
{
return x * x * (3 - 2 * x);
}

}

class NoiseArray3D
{

private const int step = 4;
private PerlinNoise3D noise;
private float[,,] map = new float[10 + 2, 10 + 2, 10 + 2];
private Vector3i offset;

public NoiseArray3D (float scale)
{
noise = new PerlinNoise3D (scale);
}

public void GenerateNoise (int offsetX, int offsetY, int offsetZ)
{
GenerateNoise (new Vector3i (offsetX, offsetY, offsetZ));
}

public void GenerateNoise (Vector3i offset)
{
this.offset = offset;

int sizeX = 10 + 2;
int sizeY = 10 + 2;
int sizeZ = 10 + 2;

for (int x=0; x<sizeX; x+=step) {
for (int y=0; y<sizeY; y+=step) {
for (int z=0; z<sizeZ; z+=step) {
Vector3i a = new Vector3i (x, y, z) + offset;
Vector3i b = a + new Vector3i (step, step, step);

float a1 = noise.Noise (a.x, a.y, a.z);
float a2 = noise.Noise (b.x, a.y, a.z);
float a3 = noise.Noise (a.x, b.y, a.z);
float a4 = noise.Noise (b.x, b.y, a.z);

float b1 = noise.Noise (a.x, a.y, b.z);
float b2 = noise.Noise (b.x, a.y, b.z);
float b3 = noise.Noise (a.x, b.y, b.z);
float b4 = noise.Noise (b.x, b.y, b.z);

for (int tx=0; tx<step  x+tx<sizeX; tx++) {
for (int ty=0; ty<step  y+ty<sizeY; ty++) {
for (int tz=0; tz<step  z+tz<sizeZ; tz++) {
float fx = (float)tx / step;
float fy = (float)ty / step;
float fz = (float)tz / step;
float ta1 = Mathf.Lerp (a1, a2, fx);
float ta2 = Mathf.Lerp (a3, a4, fx);
float ta3 = Mathf.Lerp (ta1, ta2, fy);

float tb1 = Mathf.Lerp (b1, b2, fx);
float tb2 = Mathf.Lerp (b3, b4, fx);
float tb3 = Mathf.Lerp (tb1, tb2, fy);

float val = Mathf.Lerp (ta3, tb3, fz);
map [x + tx, y + ty, z + tz] = val;
}
}
}
}
}
}
}

public float GetNoise (Vector3i pos)
{
return GetNoise (pos.x, pos.y, pos.z);
}

public float GetNoise (int x, int y, int z)
{
x -= offset.x;
y -= offset.y;
z -= offset.z;
return map [x + 1, y + 1, z + 1];
}

}
``````

and usage in Start()

``````NoiseArray3D nsah = new NoiseArray3D(0.5f);
nsah.GenerateNoise( 10, 10, 10);
Debug.Log(nsah.GetNoise(10,10,10));
``````