0

Here is my code, brick_col is updating itself as it should be, print(brick_col), tells me once the loop is complete brick_col is +1 itself, but, print (positions[i]), tells me my y value is always 0) the Vector3 isn't being updated with the value. Any ideas? Many thanks

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Brick_Spawn_Test : MonoBehaviour {

List<Vector3> positions = new List<Vector3>();
private int bricks_in_row=9;
public GameObject Brick;
private int no_in_row=9;
private int brick_col=0;
private int number_of_brick_col=2;

void Start(){
    Check_Bricks ();

}


void Check_Bricks(){
    if (brick_col != number_of_brick_col) {
        print ("not enough bricks");
        Create_Bricks ();


    }


} 


void Create_Bricks(){
    for (int i = 0; i <= bricks_in_row-1; i++)
    {
        for (int a = -4; a <= no_in_row/2; a++)
        {
            positions.Add(new Vector3(a,brick_col,0f));
        }
        print (brick_col);
        print (positions [i]);
        transform.position = positions[i];
        Instantiate(Brick,transform.position, transform.rotation);

    }
    brick_col = brick_col + 1;
    Check_Bricks ();
}

}
4
  • I don't get your problem. I only see creating, but no updating in the code you provided Commented Dec 15, 2017 at 12:00
  • 1
    it seems OP updates the position of the object and then uses that to instantiate the bricks. there's no need to do so, though. transform.position = positions[i]; Commented Dec 15, 2017 at 12:02
  • I'm not sure I understand what you mean, I'm looping through the positions list to instantiate the bricks in there different positions. Without transform.position = positions[i]; I would just get lots of bricks in the same x,y Commented Dec 15, 2017 at 12:12
  • @JackIndge I updated my answer, with a fix using all your variables, there was another issue I wasn't considering. Commented Dec 15, 2017 at 13:44

2 Answers 2

3

In your code you use the following variable as your y value

private int brick_col=0;

In your inner loop you add elements to your positions list with

positions.Add(new Vector3(a,brick_col,0f));

Without updating the brick_col until you are outside both loops.

Move this brick_col = brick_col + 1; to where you want the update to really happen and if you put it into the inner loop you will probably also want to reset it just before entering again.

Sign up to request clarification or add additional context in comments.

2 Comments

actually there is a bigger problem then just that, Instantiate will use the same positions for 9 of the bricks per column, so even if he added that he would have 9 bricks on top of each other.
It depends on what he is trying to achieve.
1

Alright honestly, you are doing some unnecessary things I will explain why as I go over it, I do things like this at times as well when I am trying to figure out what is going on, or when I am in a rush to build something I am excited to try, so starting out I will use your code and explain, then the fix then I will show another way to do this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Brick_Spawn_Test : MonoBehaviour {

List<Vector3> positions = new List<Vector3>();
private int bricks_in_row=9;
public GameObject Brick;
private int no_in_row=9;  // No need for a number in row because you have bricks in row which is the same thing.
private int brick_col=0;  // No need for this variable, as you are going to be counting anyways (in this case your i variable)
private int number_of_brick_col=2;

void Start(){
    Check_Bricks ();
}


void Check_Bricks(){  // This function is unnessary, it appears it may have been added when you were trying to fix your y issue.
    if (brick_col != number_of_brick_col) {
        print ("not enough bricks");
        Create_Bricks ();
    }
} 


void Create_Bricks(){
    for (int i = 0; i <= bricks_in_row-1; i++) // This will run 9 times.
    {
        for (int a = -4; a <= no_in_row/2; a++) // This will also run 9 times
        {
            positions.Add(new Vector3(a,brick_col,0f));
        }
        // Move all this into the inner loop.
        print (brick_col);
        print (positions [i]); // By this point you will have 9 then 18 then 27... as your inner loop this position would be positons[i * bricks_in_row + (a +4)] with how you are looping
        transform.position = positions[i]; /// This positions should be based off of the individual brick, next time around you are setting this position to the second index but by this time you have 18.
        Instantiate(Brick,transform.position, transform.rotation);
        // 
        // brick_col = brick_col + 1; This will be in the outter loop  
    }
    brick_col = brick_col + 1; // This should be before the closing bracket. not outside the loop
    Check_Bricks ();
}

}

This is how it would look, if I kept your variables and just fixed your y and positioning problems:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Brick_Spawn_Test : MonoBehaviour {

List<Vector3> positions = new List<Vector3>();
private int bricks_in_row=9;
public GameObject Brick;
private int no_in_row=9;  
private int brick_col=0;  
private int number_of_brick_col=2;

void Start(){
    Check_Bricks ();
}


void Check_Bricks(){ 
    if (brick_col != number_of_brick_col) {
        print ("not enough bricks");
        Create_Bricks ();
    }
} 


void Create_Bricks(){
    for (int i = 0; i <= bricks_in_row-1; i++) 
    {
        for (int a = -4; a <= no_in_row/2; a++) 
        {
            positions.Add(new Vector3(a,brick_col,0f));
            print (brick_col);
            print (positions [i * bricks_in_row + a +4]);
            transform.position = positions[i * bricks_in_row + a +4];
            Instantiate(Brick,transform.position, transform.rotation);
        }
        brick_col = brick_col + 1; 
    }
    Check_Bricks ();
}

}

This is a way to handle this, you can ignore the way I name variables as it is a matter of preference:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Brick_Spawn_Test : MonoBehaviour {
    [SerializeField]
    private int totalBrickColumns = 9; // Serializing it so I can use this for multiple test cases and edit the variable in the inpsector.
    [SerializeField]
    private int totalBrickRows = 2;
    [SerializeField]
    private Vector2 startingPoint = new Vector2(-4, 0);
    [SerializeField]
    private GameObject Brick;

    void Start()
    {
        CreateBricks();
    }

    void CreateBricks()
    {
       Vector2 spawnPosition = startingPoint;

       for(int x = 0; x < totalBrickColumns; ++x) // x is my column 
       {
           spawnPosition.x = startingPoint.x + x;  // the x is my offset from the startingPoint.x so if I wanted to start at -4 i just set the startingPoint.x to -4
           for(int y = 0; y < totalBrickColums; ++y) // y is my row
           { 
               spawnPosition.y = startingPoint.y + y; // the y is my offset from the startingPoint.y
               print("Brick Location: " + spawnPosition.toString());
               Instantiate(Brick,spawnPosition, transform.rotation);
           }
        }
    }
}

In regards to why your y isn't updating, is because you are not updating the variable inside of your first loop. See the comment in your code on brick_col in the Create_Brick() function.

EDIT: I noticed something I wasn't considering when I said you needed to update your outter loop, I also added a fix using only your code, with your variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.