嵌套for循环中的实例化不能达到预期目的

时间:2017-12-27 18:05:03

标签: c# for-loop unity3d instantiation minecraft

我一直在使用Unity 2017.3和C#中的Minecraft克隆游戏,我开始没事,但是当我在超级平面世界发电系统上工作时,开始出现问题。在World mono行为中,在Generate void中,有三个嵌套for循环,它们应该在0到5之间的每个可能位置生成一个新的污垢块

但它只会使一条污垢块沿Z轴伸展。

以下是PlaceableItem(附加到dirtPrefab)和World(附加到World)的代码

可放置物品类

using UnityEngine;
using System.Collections;

public class PlaceableItem : MonoBehaviour
{

    public string nameInInventory = "Unnamed Block";
    public int maxStack = 64;

    public bool destructible = true;
    public bool explodesOnX = false;
    public bool abidesGravity = false;
    public bool isContainer = false;
    public bool canBeSleptOn = false;
    public bool dropsSelf = false;

    private Rigidbody rb;

    // Use this for initialization
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        if (abidesGravity) {
            rb.useGravity = true;
        } else {
            rb.useGravity = false;
        }
    }

    private void OnDestroy()
    {
        if (dropsSelf) {
            //Drop this gameObject
        }
    }

    public void Explode() {
        if (!explodesOnX)
            return;
    }

    public void OnMouseDown()
    {
        if (isContainer && !canBeSleptOn) {
            //Open the container inventory
        } else if (canBeSleptOn && !isContainer) {
            //Make the character sleep on the item
        }
    }

    private void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(1) && destructible) {
            Destroy(gameObject);
        }
    }

}

世界级

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

public class World : MonoBehaviour {

    public GameObject dirtPrefab;

    public bool generateAutomatically = true; 

    // Use this for initialization
    void Start () {
        if (generateAutomatically) {
            Generate();
        }
    }

    // Update is called once per frame
    void Update () {

    }

    void Generate() {
        for (int x = 0; x <= 5; x++) {
            for (int y = 0; y <= 5; y++) {
                for (int z = 0; z <= 5; z++) {
                    Instantiate(dirtPrefab, new Vector3(x, y, z), Quaternion.identity);
                }
            }
        }
    }

    void RemoveAllBlocks() {
        foreach (PlaceableItem placeableItem in GetComponentsInChildren<PlaceableItem>()) {
            Destroy(placeableItem.gameObject);
        }
    }
}

提前感谢开发人员! 希望这不是一个愚蠢的问题!

1 个答案:

答案 0 :(得分:0)

[已解决]我意识到我有一个矩形变形以某种方式附着在我的污垢预制件上。删除它并添加转换有助于。