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

问题描述 投票:-2回答:1

我一直在使用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);
        }
    }
}

在此先感谢开发人员!希望这不是一个愚蠢的问题!

c# for-loop unity3d instantiation minecraft
1个回答
0
投票

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

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.