- Home /
if statement isnt working
Im trying to make a cube world and i am trying to check if the block above the current block is =0 or =1 but the if statement isnt working. Even if the block above is 1 it is still returning as if it is 0. I dont know what is wrong. Any suggestions on what i did wrong? Thanks
 using UnityEngine;
 using System.Collections;
 
 public class DataScript : MonoBehaviour 
 {
     public byte[ , , ] data;
     
     public int worldX = 10, worldY = 10, worldZ = 10;
     
     public GameObject block;
     
     void Start ()
     {
         data = new byte[worldX, worldY, worldZ];
         for (int x=0; x<worldX; x++)
         {
             for (int y=0; y<worldY; y++)
             {
                 for (int z=0; z<worldZ; z++)
                 {
                     if (y<2)
                     {
                         data[x, y, z] = 1;
                     }else{
                         data[x, y, z] = 0;
                     }
                     
                     if (data[x, y, z] == 1)
                     {
                         GameObject cube = Instantiate(block, new Vector3(x, y, z), Quaternion.identity) as GameObject;
                     }
                     
                     if (y+1 < worldY)
                     {
                         if (data[x, y, z] != 0)
                         {
                             if (data[x, y+1, z] == 0) // This is the line that isnt working
                             {
                                 print("Nothing Above");
                             }
                         }
                     }
                 }
             }
         }
     }
 }
 
Answer by revolute · Oct 02, 2014 at 01:56 AM
It is due to the fact that the voxel above has not been set yet. You currently make a voxel at y=a but since your for loop is incremental loop, y=a+1 is not yet reached.
Simply put, when you make a voxel at (x,y,z), voxel at (x,y+1,z) has not been initialized by you and is by default set to 0.
O ok I understand. So I should do the for loop the do another for loop after?
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Error when checking if float is a whole number 2 Answers
How to get around adding loads of "if" commands in my script 2 Answers
GameObject tag to if condition 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                