在運行時,我想將一個Gameobject組件複製到另一個gameobject。在運行時將一個gameobject組件添加到另一個具有值的gameobject中
在我的情況下,我有一個攝像頭,其中多個腳本添加了值設置。
我想在運行時添加到我的另一臺攝像機中的相同組件。到目前爲止,我已經嘗試過這樣做,獲取對象的所有組件然後嘗試添加,但它不起作用。
Component[] components = GameObject.Find("CamFly").GetComponents(typeof(Component));
for(var i = 0; i < components.Length; i++)
{
objCamera.AddComponent<components[i]>();
///error in above line said adds a component class named/calss name to the gameobject
}
我想添加一個gameobject組件到另一個gameobject,即我使用上面的代碼。 –
好吧,我做克隆,然後它仍然不工作 –
然後,你應該複製粘貼從我鏈接的答案(由莎法給出)的代碼。至於你的第二條評論:你克隆了GO,哪些不起作用?上面的代碼永遠不會工作(我解釋了爲什麼)。然而,通過克隆,新對象將具有與舊對象相同的組件。 – 2016-06-28 08:22:48