尝试在脚本中将子级设置为我的GameObject时发生错误 [英] An error occurs when trying to set a child to my GameObject in script

查看:134
本文介绍了尝试在脚本中将子级设置为我的GameObject时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GameObject enemy = Instantiate(spawnObject,spawnPosition,spawnObject.transform.rotation) as GameObject;
enemy.transform.parent = transform;

当我在游戏模式下测试游戏时,上面的代码会产生预期的结果,但是我收到了以下错误消息:

The above code generates the expected result when I test my game in game mode, however I'm gettting this error message:

为了防止数据损坏,禁止设置驻留在预制件中的变换的父级."

"Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."

是的,spawnObject变量包含一个预制件,但是我认为创建一个新的GameObject应该可以解决该问题?

Yes, the spawnObject variable contains a prefab, however creating a new GameObject should have fixed the problem, I assume?

推荐答案

检查您的变换"变量是否实际上来自游戏对象而不是预制对象.

Check if your "transform" variable is actually from a gameobject and not from a prefab.

var transform = somePrefab.transform;
enemy.transform.parent = transform; // this won't work

var transform = someOtherGameObject.transform;
enemy.transform.parent = transform; // this will

也许您可以提供有关您的变换变量的来源的更多信息.

Maybe you could give some more information about where that transform variable of yours comes from.

这篇关于尝试在脚本中将子级设置为我的GameObject时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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