首页 > 解决方案 > 当我使用实例化语法 C# 按下播放按钮时,Unity 冻结

问题描述

每次我统一按下播放按钮时,它只是坐在那里什么都不做,我认为它与实例化语法有关,这是我的代码,这是我运行的唯一脚本。我试图简单地制作我的游戏对象的网格。如果有人知道为什么那会很棒,或者如果不是,也许您知道更好的方法。谢谢!!

    using UnityEngine;

public class Script2 : MonoBehaviour
{
    public GameObject Sprite;

    void Start()
    {
        Generate();
    }
    public void Generate()
    {
        float width = Sprite.transform.lossyScale.x;
        float height = Sprite.transform.lossyScale.y; 
        for (int y = 0; y <= 100; y += 10)
        {
           for (int x = 0; x <= 100; y += 10)
            {
                GameObject Square = Sprite;
                Instantiate(Square, new Vector2(x * width, y * height), Quaternion.identity);
            }
        }
    }
}

标签: c#unity3dinstantiation

解决方案


你的 x 循环有一个错误,所以它陷入了无限循环。应该是 x+=10

for (int x = 0; x <= 100; x += 10)
{
    GameObject Square = Sprite;
    Instantiate(Square, new Vector2(x * width, y * height), Quaternion.identity);
}

推荐阅读