C# 如何向实例化的游戏对象添加脚本
基本上,我必须将脚本添加到实例化的预置中,但原始预置无法附加脚本,因此我尝试使用C# 如何向实例化的游戏对象添加脚本,c#,unity3d,C#,Unity3d,基本上,我必须将脚本添加到实例化的预置中,但原始预置无法附加脚本,因此我尝试使用gameObject.AddComponent添加脚本,因为至少在inspector中,脚本显示为组件 这是我的代码行: ObjectInstance.AddComponent(脚本测试); 它给了我以下错误: The name "ScriptTest" does not exist in the current context. 我是否必须在开始时声明它:publicscript Scrip
gameObject.AddComponent添加脚本,因为至少在inspector中,脚本显示为组件
这是我的代码行:
ObjectInstance.AddComponent(脚本测试);
它给了我以下错误:
The name "ScriptTest" does not exist in the current context.
我是否必须在开始时声明它:publicscript ScriptTest
?首先,您应该这样做
ObjectInstance.AddComponent<ScriptTest>();
能否显示ScriptTest
的内容。。这是c#,尤其是与单一行为组件的统一。。要附加到对象的任何内容都应作为公共类ScriptTest:MonoBehavior{…}
写入,并位于一个名称完全相同的文件中(ScriptTest.cs
,其中.cs
不在Unity中显示)
using ScriptTestNamespace;