You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity按钮Inspector的OnClick仅显示MonoScript问题求助

Unity按钮OnClick下拉仅显示MonoScript的解决方法

开发叙事类决策RPG时,按钮Inspector面板的OnClick原本能正常绑定函数,添加切换场景的SceneHandler脚本后,下拉菜单仅显示MonoScript。移除该脚本后,其他按钮的脚本也出现同样问题,回退旧存档也无法解决。

解决步骤

  • 清理项目缓存并重新编译
    1. 关闭Unity编辑器
    2. 删除项目根目录下的Library文件夹
    3. 重新打开Unity,等待项目自动重新编译完成
  • 排查编译错误
    打开Unity控制台面板,修复所有编译错误(包括隐性的语法错误、引用缺失等),确保所有脚本编译通过
  • 重新挂载脚本组件
    对出现问题的按钮对象,先移除已挂载的脚本组件,再重新添加组件并重新绑定OnClick函数
  • 验证脚本命名一致性
    确认脚本文件名与内部类名完全匹配(例如Activator.cs对应public class Activator),大小写也需一致
  • 重置编辑器布局
    点击Unity顶部菜单栏Window > Layouts > Reset to Default,恢复默认布局后再尝试函数绑定

相关脚本代码

Activator.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Activator : MonoBehaviour
{
public GameObject NextDialogue;
public GameObject CurrentDialogue;
public GameObject NextButton;
public GameObject OtherNextButton;

public void Activation()
{      
    if (NextDialogue != null)
    {
        NextDialogue.SetActive(true);
    }
    if (CurrentDialogue != null)
    {
        CurrentDialogue.SetActive(false);
    }
    if (NextButton != null)
    {
        NextButton.SetActive(false);
    }
    if (OtherNextButton != null)
    {
        OtherNextButton.SetActive(true);
    }
    
}
}

SceneHandler.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneHandler : MonoBehaviour
{
public string SceneName;

public void NextScene()
{
    SceneManager.LoadScene(SceneName);
}
}

内容的提问来源于stack exchange,提问作者RGVC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 19:12:01