Instanciated gameobject的行为就像是预制
问题描述:
我在模块化太空船上工作,可以由玩家自定义。一艘船有固定点,固定模块,反过来可以容纳更多模块等。等等。Instanciated gameobject的行为就像是预制
这个时候一个硬点的代码看起来很简单;
public class Hardpoint : MonoBehaviour {
public GameObject holds; //this holds the prefab
public ComponentObject.Type[] canHold;
private GameObject heldInstance; //this holds an instance of the prefab
public void SpawnComponent() {
Clear();
heldInstance = Instantiate(holds, transform.position, transform.rotation) as GameObject;
heldInstance.transform.SetParent(transform);
}
public void RollThroughDecompression(CompressedComponent c) {
heldInstance.GetComponent<ComponentObject>().Decompress(c);
}
public void Clear() {
foreach (Transform child in transform)
{
Destroy(child.gameObject);
}
}
}
但是,它所有的行为都像是预制。因为错误消息我越来越有:
Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true);
和
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
我完全在这一点上失去了。任何人都可以指出我正确的方向,为什么这些错误不断弹出?编辑: 一些截图。
预期的结果:
实际结果:
这一切都开始于EmptyHardpoint
。如你所见,它确实产生了驾驶舱,并将EmptyHardpoint
设置为父母。但这就是乐趣结束的地方。进一步Gameobjects
被视为预制。
减压代码:
public void Decompress(CompressedComponent c) {
componentType = (Type)Enum.Parse(typeof(Type), c.componentType);
componentNumber = c.componentNumber;
UpdateHardPoints();
GameObject[] typeRepository = GetRepository(componentType);
//update children
int point = 0;
foreach (Transform child in typeRepository[componentNumber].transform)
{
Hardpoint hardpoint = child.GetComponent<Hardpoint>();
if (hardpoint != null) {
Debug.Log("Hardpoint found in " + child.transform.parent.name);
if (c.hardpoints[point] != null) {
//get the hardpoint's repository
GameObject[] hardpointRepo = GetRepository((Type)Enum.Parse(typeof(Type), c.hardpoints[point].componentType));
//set the hardpoint to hold this object
hardpoint.holds = hardpointRepo[c.hardpoints[point].componentNumber];
hardpoint.SpawnComponent();
hardpoint.RollThroughDecompression(c.hardpoints[point]);
point++;
}
}
}
}
CompressedComponent
甲仅仅保持模块化对象的类型。它从场景中的存储库加载(我知道它的凌乱,我会在稍后处理)。
答
感谢CùĐứcHiếu我想通了,我正在寻找错误的领域。在减压期间,我正在循环实际的预制件,而不是变形。固定代码:
foreach (Transform child in transform)
{
Hardpoint hardpoint = child.GetComponent<Hardpoint>();
if (hardpoint != null) {
Debug.Log("Hardpoint found in " + child.transform.parent.name);
if (c.hardpoints[point] != null) {
//get the hardpoint's repository
GameObject[] hardpointRepo = GetRepository((Type)Enum.Parse(typeof(Type), c.hardpoints[point].componentType));
//set the hardpoint to hold this object
hardpoint.holds = hardpointRepo[c.hardpoints[point].componentNumber];
hardpoint.SpawnComponent();
hardpoint.RollThroughDecompression(c.hardpoints[point]);
point++;
}
}
}
您能否向我们展示导致这些错误的示例船的场景层次结构的屏幕截图?只有当您调用Clear()时才抛出这些错误? – Serlite
你'实例化'你的'Hardpoint'吗? –
@ Serlite'Clear()'只抛出第一条错误信息,我在设置父项时得到第二条错误信息。 @CùĐứcHiếu我不知道说实话。我使用网络和作为播放器,我正在使用'hardpoint'和一个额外的脚本来控制运动。 –