Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Feedback Trying to make a series of random walls on my instantiated board

Discussion in 'Scripting' started by Jinngineer, Nov 18, 2021.

  1. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    Ok the title is not that clear on what im trying to do.

    So i wrote some code instantiating a a grid pattern in a rectaguar area, now im trying to on top of that grid floor to make some walls be instantiated, and i only want a variable percent to be covered.

    Now i wrote some code that i thought would be correct. but nothing instantiates on top of my floor.


    I thought that the code didnt work because maybe the grid had no child components till after the GameBoard(); had finished, so i used a if statement to delay the Wall(); from initializing till all the grid had been layed...

    Would love some suggestions :)

    Have a great day and thanks for any help :)

    Code (CSharp):
    1.  
    2.  
    3. public class GameManager : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     GameObject cube, cube2, grid, x;
    7.  
    8.     Vector3 position = new Vector3(0f, 0f, 0f);
    9.  
    10.     [SerializeField]
    11.     Rect board;
    12.  
    13.     [SerializeField, Range(10f, 80f)]
    14.     float percernt;
    15.  
    16.  
    17.  
    18.  
    19.     void Start()
    20.     {
    21.         GameBoard();
    22.      
    23.  
    24.     }
    25.  
    26.     private void Update()
    27.     {
    28.        
    29.     }
    30.  
    31.     void GameBoard()
    32.     {      
    33.             for(int i=0; i < board.x; i=i+1)
    34.             {
    35.                 position.x = i;
    36.  
    37.                 for(int o =0; o< board.y; o = o + 1)
    38.                 {
    39.                 position.z = o;
    40.  
    41.                 CheckerBoard();
    42.  
    43.  
    44.                     if(i == board.x && o == board.y)
    45.                     {
    46.                     Walls();
    47.                     }
    48.                 }                                        
    49.             }                
    50.     }
    51.  
    52.     void CheckerBoard()
    53.     {      
    54.         if (position.x%2 == 0)
    55.         {
    56.             if(position.z%2 == 1)
    57.             {
    58.                 GameObject cubeEvenOdd = Instantiate(cube, position, Quaternion.identity);
    59.                 cubeEvenOdd.transform.SetParent(grid.transform);
    60.             }
    61.             else if(position.z % 2 == 0)
    62.             {
    63.                 GameObject cubeEvenEven = Instantiate(cube2, position, Quaternion.identity);
    64.                 cubeEvenEven.transform.SetParent(grid.transform);
    65.             }
    66.          
    67.         }
    68.  
    69.         else if (position.x % 2 == 1)
    70.         {
    71.             if (position.z % 2 == 0)
    72.             {
    73.                 GameObject cubeOddEven =   Instantiate(cube, position, Quaternion.identity);
    74.                 cubeOddEven.transform.SetParent(grid.transform);
    75.  
    76.             }
    77.  
    78.             else if (position.z % 2 == 1)
    79.             {
    80.                 GameObject cubeOddOdd =   Instantiate(cube2, position, Quaternion.identity);
    81.                 cubeOddOdd.transform.SetParent(grid.transform);
    82.             }
    83.  
    84.         }
    85.  
    86.     }
    87.  
    88.     void Walls()
    89.     {
    90.         for(int j = (x.transform.childCount / grid.transform.childCount*100); j<percernt; j= ((x.transform.childCount+1/grid.transform.childCount)*100))
    91.         {
    92.  
    93.  
    94.             Vector3 pos = new Vector3(position.x, 0.5f, position.z);
    95.  
    96.  
    97.             GameObject wall = Instantiate(cube, pos, Quaternion.identity);
    98.             wall.transform.SetParent(x.transform);
    99.         }
    100.  
    101.  
    102.     }
    103.  
    104. }
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    I didn't really read into what your code was doing too much, but on line 90, the really long one, you're doing division math with ints. I don't really know what you're trying to achieve, but I'm guessing that you don't want it to round after every calculation.
     
  3. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    oh god! youre right... @RadRedPanda Hmm, i think an example would work best to explain what i want.

    one part of my script makes a game board (lets a ssume a square 10x10)
    i want on top of that floor to overlay a percentage (say for eg. 60%) so that i have in a random position on top of the floor walls to make a arena of sorts.