在Unity中实例化我的游戏对象会在我运行它时导致项目崩溃.我做错了什么?



我正在制作我的游戏《Maltrov》的平台部分。我有一个脚本(如下),应该复制一个平台5次,每次克隆是在一个新的地方。每当我将脚本附加到我的平台并将其分配为父变量和Gameobject变量时,项目就会崩溃。是否有一种方法使代码工作?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Duplicateplatform : MonoBehaviour
{
public GameObject newGameObject;
public Transform parent;
Vector3 newPosition;
public Quaternion newRotation;
public float xValue = 20f;
public float yValue = 20f;

// Start is called before the first frame update
void Start()
{
for (int i = 0; i < 5; i++)
{
xValue += 20f;
yValue += 20f;
duplicateObject(xValue, yValue);
}
}

public void duplicateObject(float xValue,float yValue)
{
// create (duplicate, in a new position, at a new rotation to the parent)
newPosition = new Vector3(xValue, yValue, 0);
Instantiate(newGameObject,newPosition,newRotation,parent); 
}

}

我尝试将平台附加到一个空的Gameobject上,然后将平台的脚本附加到空的Gameobject上,但项目仍然崩溃了。

我试过你的代码,它在我的工作很好。因此,问题出在别处。
检查你的检查器怎么样?
如果你得到null错误,检查你是否插入了newGameObject和Parent。
如果你的编辑器冻结了,那可能是因为你添加了游戏对象作为实例化对象,并且它处于无限循环中。

最新更新