实例化的游戏对象的行为就像是预制的 [英] Instanciated gameobject acts like it's a prefab

查看:228
本文介绍了实例化的游戏对象的行为就像是预制的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在由玩家定制的模块化太空飞船上工作.船上有一些硬点,这些硬点可以容纳模块,而这些模块又可以容纳更多模块等.

Im working on modular space ships, customizable by the player. A ship has hardpoints, which hold modules, which in turn can hold more modules, etc. etc.

此时的难题代码看起来非常简单;

The code of a hardpoint at this moment looks very simple;

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

但是,这一切都像是一个预制件.因为我收到的错误消息是:

however, it all acts like it's a prefab. because error messages i'm getting are:

Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true);

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.

我现在完全迷失了.任何人都可以为我指出正确的方向,为什么这些错误不断弹出?

I'm completely lost at this point. Can anyone point me in the right direction as to why these errors keep popping up?

一些截图.

预期结果:

实际结果:

所有内容均以EmptyHardpoint开头.如您所见,它确实生成了驾驶舱,并将EmptyHardpoint设置为父级.但这就是乐趣的终点.进一步Gameobjects就像是预制件一样处理.

It all starts with the EmptyHardpoint. as you can see, it does spawn the cockpit, and sets the EmptyHardpoint as parent. But that's where the fun ends. further Gameobjects are handled as though they are prefabs.

减压代码:

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

A CompressedComponent仅保存模块化对象的类型.

A CompressedComponent merely holds the type of the modular object. which get loaded from a repository in the scene (i know its messy, i'll work it out later.)

推荐答案

感谢CùĐứcHiếu,我发现自己在找错地方了.在解压缩期间,我遍历了实际的预制件,而不是进行遍历.固定代码:

Thanks to Cù Đức Hiếu i figured out i was looking in the wrong area. during decompression i was looping through the actual prefab, instead of the transform. Fixed Code:

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

这篇关于实例化的游戏对象的行为就像是预制的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆