简体   繁体   English

无法在 Unity3d 中向对象添加新脚本

[英]Can't add new Scripts to Objects in Unity3d

I tried to add a script to a GameObject in the Hierarchy View in Unity.我尝试在 Unity 的层次结构视图中向游戏对象添加脚本。 I dragged it before to my Scripts-Folder.我之前将它拖到我的脚本文件夹中。 I got the following message:我收到以下消息:

Can't add script behaviour AssemblyInfo.cs.无法添加脚本行为 AssemblyInfo.cs。 The script needs to derive from MonoBahaviour!该脚本需要从 MonoBahaviour 派生!

I tested it with a new standard script - (Maybe copy and paste would bypass the problem) but also the new standard script gives me the message:我用一个新的标准脚本测试了它 - (也许复制和粘贴会绕过这个问题)但新的标准脚本也给了我这样的信息:

No MonoBehaviour scripts in the file, or their names do not match the filename.文件中没有 MonoBehaviour 脚本,或者它们的名称与文件名不匹配。

I looked it already up and the following solutions don't apply to this case.我已经查过了,以下解决方案不适用于这种情况。

  • The Script name is named exactly like the Monobehaviour Class脚本名称的命名与 Monobehaviour 类完全相同
  • Script Class inherits from the Monobehaviour Class脚本类继承自 Monobehaviour 类
  • There are no typos.没有错别字。 (Class name doesn't contain spaces) (类名不包含空格)
  • I closed and reopend Unity我关闭并重新打开了 Unity
  • Reset doesn't appear to work重置似乎不起作用

You can check both scripts (which are basically the same) below:您可以检查下面的两个脚本(基本相同):

检查员 1 的屏幕截图

检查员 2 的屏幕截图

I use Windows 10 Version 1909 and Unity 2018.4.10f as well a Coding on Visual Studio Code.我使用 Windows 10 Version 1909 和 Unity 2018.4.10f 以及 Visual Studio Code 上的编码。

Maybe you know what's going on.也许你知道发生了什么。 Thanks谢谢

[Edit] There have been some error messages in the Console. [编辑] 控制台中出现了一些错误消息。

Generating diff of this object for undo because the type tree changed.This happens if you have used Undo.RecordObject when changing the script property. Please use Undo.RegisterCompleteObjectUndo

OnGUIDepth changed: was 0 is 3. Event type was 0

It is not a very good answer because I can't deliberately reproduce the answer as well I can't reproduce the problem.这不是一个很好的答案,因为我无法故意重现答案,也无法重现问题。 But now I again can add scripts to GameObjects in the Hierarchy View.但是现在我又可以在层次结构视图中向游戏对象添加脚本了。

As in the following Link of a Unity tutorial about: Unity Project Folder Structure there exist the Visual Studio Solution File : .sln which contains information of the project enviroment and project state.如以下关于 Unity 教程的链接: Unity 项目文件夹结构存在Visual Studio 解决方案文件.sln ,其中包含项目环境和项目状态的信息。 Futhermore there is the Visual Studio C# Project File : .csproj as which contains project settings and references to files used by the project.此外,还有Visual Studio C# 项目文件.csproj ,其中包含项目设置和对项目使用的文件的引用。

Infos are from https://fileinfo.com/extension/sln and https://fileinfo.com/extension/csproj信息来自https://fileinfo.com/extension/slnhttps://fileinfo.com/extension/csproj

I deleted them.我删除了它们。 (Don't worry Unity autogenerates them.) (别担心 Unity 会自动生成它们。)

Nevertheless there was no immediate improvement.尽管如此,并没有立即改善。 I deleted all script files and tried to drag and drop a new script file.我删除了所有脚本文件并尝试拖放一个新的脚本文件。 Upon this action Unity crashed.执行此操作后,Unity 崩溃了。 After restarting Unity scripts could be created which worked again - or should I say Unity worked again because there was never a problem with the scripts.重新启动 Unity 脚本后,可以创建再次运行的脚本 - 或者我应该说 Unity 再次运行,因为脚本从来没有出现过问题。

I know, it's a botched solution but if this problem occurs to you and you tried all the bullet points in the question, (a) deleting .sln and .csproj files, (b) restarting Unity and (c) importing/creating new scripts might work.我知道,这是一个拙劣的解决方案,但如果您遇到此问题并且您尝试了问题中的所有要点,(a) 删除.sln.csproj文件,(b) 重新启动 Unity 和 (c) 导入/创建新脚本可能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM