Search Unity

Question Instantiated cube y value different than Debug.Log y value output using same variable?

Discussion in 'Scripting' started by wardhk, Apr 21, 2023.

  1. wardhk

    wardhk

    Joined:
    Aug 7, 2021
    Posts:
    3
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Ex = System.Exception;
    5.  
    6. public class TerrainGen : MonoBehaviour
    7. {
    8.     public int width = 16;
    9.     public int height = 50;
    10.  
    11.     public GameObject myPrefab;
    12.  
    13.     private MeshFilter meshFilter;
    14.     private static float noiseScale = .05f;
    15.  
    16.     Mesh mesh;
    17.     Vector3[] vertices;
    18.     int[] triangles;
    19.  
    20.     void Start()
    21.     {
    22.  
    23.         GeneratePerlinNoiseMesh(0, 0);
    24.     }
    25.  
    26.     public void GeneratePerlinNoiseMesh(int offsetX, int offsetZ) {
    27.         mesh = new Mesh();
    28.         GetComponent<MeshFilter>().mesh = mesh;
    29.         vertices = new Vector3[(width+1) * (width+1)];
    30.         for (int i = 0, x = 0; x <= width; x++) {
    31.             for (int y = 0; y <= height; y++) {
    32.                 for (int z = 0; z <= width; z++) {
    33.                     if (getBlock(x + offsetX * width, y, z + offsetZ * width) && (!isSurrounded(x + offsetX * width, y, z + offsetZ * width))) {
    34.                         Debug.Log(y);
    35.                         GameObject cube = Instantiate(myPrefab, new Vector3(x + offsetX * width, y, z + offsetZ * width), Quaternion.identity);
    36.                    
    37.                         Debug.Log(i + "" +  new Vector3(x, y, (i%(width + 1))));
    38.                         vertices[i] = new Vector3(x, y, (i%(width + 1)));
    39.                         i++;
    40.                     }
    41.                 }
    42.             }
    43.  
    44.         }
    45.         triangles = new int[width * width* 6];
    46.  
    47.         int vert = 0;
    48.         int tris = 0;
    49.  
    50.         for (int x = 0; x < width; x++) {
    51.             for (int z = 0; z < width; z++) {
    52.                 triangles[tris + 0] = vert + 0;
    53.                 triangles[tris + 2] = vert + width + 1;
    54.                 triangles[tris + 1] = vert + 1;
    55.                 triangles[tris + 3] = vert + 1;
    56.                 triangles[tris + 5] = vert + width + 1;
    57.                 triangles[tris + 4] = vert + width + 2;
    58.  
    59.                 vert++;
    60.                 tris += 6;
    61.             }
    62.             vert++;
    63.         }
    64.         UpdateMesh();
    65.     }
    66.  
    67.     void UpdateMesh() {
    68.  
    69.         mesh.Clear();
    70.  
    71.         mesh.vertices = vertices;
    72.         mesh.triangles = triangles;
    73.  
    74.         mesh.RecalculateNormals();
    75.     }
    76.  
    77.     public static bool getBlock(int x, int t, int z) {
    78.         float density = ((Perlin3D(x * noiseScale, t * noiseScale, z * noiseScale)*2)-1);
    79.         float yAverage = (t*2) / 100.0f;
    80.         yAverage = (yAverage * 2) - 1;
    81.    
    82.         //Increase yAverage divison for more moutains, decrease for flatter
    83.         density = (density + (yAverage/1.2f)) / 2;
    84.         if (density >= 0) {
    85.             return true;
    86.         } else {
    87.             return false;
    88.         }
    89.  
    90.     }
    91.     public static float Perlin3D(float x, float t, float z) {
    92.         float ab = Mathf.PerlinNoise(x, t);
    93.         float bc = Mathf.PerlinNoise(t, z);
    94.         float ac = Mathf.PerlinNoise(x, z);
    95.  
    96.         float ba = Mathf.PerlinNoise(t, x);
    97.         float cb = Mathf.PerlinNoise(z, t);
    98.         float ca = Mathf.PerlinNoise(z, x);
    99.  
    100.         float abc = ab + bc + ac + ba + cb + ca;
    101.         return abc / 6f;
    102.     }
    103.  
    104.     bool isSurrounded(int x, int t, int z) {
    105.         int[] dx = {-1, 1, 0, 0, 0, 0};
    106.         int[] dy = {0, 0, -1, 1, 0, 0};
    107.         int[] dz = {0, 0, 0, 0, -1, 1};
    108.  
    109.         for (int i = 0; i < 6; i++) {
    110.             if (!getBlock(x + dx[i], t + dy[i], z + dz[i])) {
    111.                 return false;
    112.             }
    113.         }
    114.  
    115.         return true;
    116.     }
    117.  
    118. }



    In my GeneratePerlinNoiseMesh function I instantiate the cube object using
    Instantiate(myPrefab, new Vector3(x + offsetX * width, y, z + offsetZ * width), Quaternion.identity);. After I create a vertex at the same location as the cube using
    vertices = new Vector3(x, y, (i%(width + 1)));. If you look at the image Debug.Log(i + "" + new Vector3(x, y, (i%(width + 1)))); outputs y value is 22 for vertex number 18 and Debug.Log(y); is also outputting 22 yet the position y for the cube instantiated at the same X and Z position is 23 using the same y variable as the vertex? Both X and Z are the same for all vertices but the y value is 1 off for some of them like showed in the image... anyone know the problem here? I have tried multiple different strategies to fix it and have spent plenty of time looking at the code with no success.
     

    Attached Files:

  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    Make sure you haven't overriden the default cube. Maybe you have moved it by 1 by mistake. That should fix automatically when you restart Unity. So try that before anything else.
     
  3. wardhk

    wardhk

    Joined:
    Aug 7, 2021
    Posts:
    3
    In the image you can see some cubes line up correctly with the vertices in the mesh and some are off by 1, I double checked and default cube is at 0 location at xyz, just smaller in scale. It is a very odd problem to me because nothing makes sense even when debugging.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Then you haven't finished debugging yet!

    One must work through the six stages of debugging:

    DENIAL. That can’t happen.
    FRUSTRATION. That doesn’t happen on my machine.
    DISBELIEF. That shouldn’t happen.
    TESTING. Why does that happen?
    GOTCHA. Oh, I see.
    RELIEF. How did that ever work?

    You may laugh now, but it will really seem hilarious to you after the 1000th time it happens.

    My money is on your unusual creation of a class-level Mesh variable, or else your vertices or triangles shared arrays. Why not just pass these around, such as to the UpdateMesh() method? I'm guessing you're sharing or reusing something you don't realize.

    In any case, time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.