Unity C#脚本,Vector3没有更新

时间:2017-12-15 11:57:04

标签: c# loops unity3d

这是我的代码,brick_col正在更新自己,print(brick_col),告诉我一旦循环完成,brick_col本身就是+1,但是,print(positions [i]),告诉我我的y值始终为0)Vector3未使用该值进行更新。有任何想法吗?非常感谢

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 ();
}

}

2 个答案:

答案 0 :(得分:3)

在您的代码中,您使用以下变量作为y值

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.2/chosen.jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.2/chosen.css" />

<select class="chosen-select" multiple>
<option>1</option>
<option>2</option>
<option>3</option>
</select>

<a>Update</a>

在内部循环中,您可以使用

将元素添加到您的位置列表中
private int brick_col=0;

在没有更新brick_col之前,直到你在两个循环之外。

移动positions.Add(new Vector3(a,brick_col,0f)); 到你希望更新真正发生的地方,如果你把它放到内循环中,你可能还想在重新进入之前重置它。

答案 1 :(得分:1)

老实说,你正在做一些不必要的事情,我会解释为什么当我过去的时候,我也会做这样的事情,当我想弄清楚发生了什么,或者当我匆忙构建一些我很兴奋尝试的东西,所以我将使用你的代码并解释,然后修复然后我将展示另一种方法:

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 ();
}

}

如果我保留你的变量并修正你的y和定位问题,这就是它的样子:

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 ();
}

}

这是一种处理这种情况的方法,你可以忽略我命名变量的方式,因为它是一个偏好的问题:

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);
           }
        }
    }
}

关于y为什么不更新的原因,是因为你没有在第一个循环中更新变量。请参阅brick_col函数{/ 1}}中代码中的注释。

编辑:我注意到当我说你需要更新你的outter循环时我没有考虑的事情,我还使用你的代码和你的变量添加了一个修复。