Unity保存C#脚本后出现CS1061错误,该如何修复?
Unity FPS脚本编译错误:CS1061 'enable'/'disable' 未定义修复方案
问题场景
跟着FPS游戏制作教程操作,保存InputManager.cs脚本后Unity弹出两个编译错误,代码完全照教程编写,但编辑器内部分代码未高亮。
错误信息
Assets\Scripts\InputManager.cs(23,16): error CS1061: 'PlayerInput.OnFootActions' does not contain a definition for 'enable' and no accessible extension method 'enable' accepting a first argument of type 'PlayerInput.OnFootActions' could be found (are you missing a using directive or an assembly reference?) Assets\Scripts\InputManager.cs(27,16): error CS1061: 'PlayerInput.OnFootActions' does not contain a definition for 'disable' and no accessible extension method 'disable' accepting a first argument of type 'PlayerInput.OnFootActions' could be found (are you missing a using directive or an assembly reference?)
问题原因
Unity输入系统(Input System)API版本更新:旧版API中小写的enable()/disable()方法,在新版中已改为符合C#规范的大写开头Enable()/Disable(),教程使用的是旧版API,导致代码不兼容。
修复步骤
- 打开
InputManager.cs脚本,定位到第23行和第27行 - 将
onFoot.enable();替换为onFoot.Enable(); - 将
onFoot.disable();替换为onFoot.Disable(); - 保存脚本,返回Unity等待编译完成
补充说明
代码高亮异常是因为编辑器无法识别未定义的小写方法名,修复后高亮会自动恢复正常。
内容的提问来源于stack exchange,提问作者Kate Z
相关产品推荐
相关产品推荐

