当我使用实例化语法 C# 按下播放按钮时,Unity 冻结

Unity Freezes When I Press The Play Button With The Instantiate Syntax C#

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

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

我最好的解决方案是我在创建预制件网格时使用的方法,即这种方法

using UnityEngine;

public class Class1 : MonoBehaviour
{
    public GameObject Prefab;

    private void Start ()
    {

        Generate ();

    }

    public void Generate ()
    {

        float width = Prefab.transform.lossyScale.x;
        float height = Prefab.transform.lossyScale.y;

        for ( int y = 0; y < 100; y++ )
        {

            for ( int x = 0; x < 100; x++ )
            {

                Vector3 position = new Vector3 ( x * width, y * height );

                Instantiate<GameObject> ( Prefab, position, Quaternion.identity );

            }

        }

    }

}

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

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