【问题标题】:Instantiate inside nested for loops does not do what it is intended to在嵌套的 for 循环中实例化并没有达到预期的效果
【发布时间】:2017-12-27 18:05:03
【问题描述】:

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

但它只会使一条沿 Z 轴延伸的泥块。

这里是 PlaceableItem(attached to dirtyPrefab) 和 World(attached to 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);
        }
    }
}

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

【问题讨论】:

  • 您必须通过缩进四个空格来格式化源代码。否则它会被降价弄乱。您可以阻止选择并使用问题编辑器顶部的工具栏对其进行格式化。 Ctrl+K 也会缩进选定的文本。
  • 它会实例化 125 个游戏对象吗?您是否可以提供场景编辑器、实例化块的屏幕截图?
  • 实际216(6*6*6)
  • 调试时您在 Inspector 窗口中看到了多少对象?看看你是否有 216(因为你有
  • @Andrew 请发布您的场景层次结构的屏幕截图,以及dirtPrefab 及其上所有组件的屏幕截图。

标签: c# for-loop unity3d instantiation minecraft


【解决方案1】:

[已解决] 我意识到我有一个 recttransform 以某种方式连接到我的污垢预制件上。删除它并添加一个转换反而会有所帮助。

【讨论】:

    猜你喜欢
    • 2021-11-03
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多