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

Unity重启后可交互对象OnInteract方法无法触发求助

问题修复方案:Unity可交互对象OnInteract无响应

先解决脚本启用即禁用的语法错误

你标记为//b的两行代码写在了类外部,这是C#语法错误,Unity会直接禁用报错的脚本。把这两行移到SelectableObject类内部:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class SelectableObject : MonoBehaviour
{
    // 移到类内部
    NewInputActions inputActions;
    private float interInput;

    public GameObject player;
    public OutlineMaker outl;
    private bool isOutActive;

    // ... 其他代码
}

修复输入绑定逻辑

原Awake方法未初始化输入动作实例,也未启用对应Action Map,导致输入事件绑定完全无效。修正后的Awake写法:

private void Awake()
{
    // 实例化输入动作对象
    inputActions = new NewInputActions();
    // 启用对应的Action Map(这里是你定义的Other)
    inputActions.Other.Enable();

    // 交互按键只需绑定performed事件(按下完成时触发)
    inputActions.Other.Interact.performed += ctx => OnInteract();
}

// 记得在脚本禁用时解绑事件并关闭输入,避免内存泄漏
private void OnDisable()
{
    inputActions.Other.Interact.performed -= ctx => OnInteract();
    inputActions.Other.Disable();
}

排查OnInteract触发的核心问题

玩家移动功能正常,说明输入系统全局无问题,重点检查以下几点:

  • 输入绑定方式:
    • 如果用Input System的「Send Messages」触发:确保输入动作的Action Type设为Button,Behavior选择Send Messages/Broadcast Messages,且目标对象能接收消息
    • 如果用射线检测触发:确认玩家脚本里的射线检测能正确命中该可交互对象,且按下E时会调用OnInteract()
  • 组件状态:
    • 可交互对象的SelectableObject脚本必须处于启用状态
    • 对象的碰撞体(如BoxCollider)必须启用,且Layer设置在射线检测的范围内
    • player变量必须在Inspector面板正确赋值,空引用会直接中断逻辑
  • Debug验证:
    • 把OnInteract里的Debug.LogWarning("DETECTED E");放在最开头,确认方法是否真的没触发;如果没输出,说明触发逻辑有问题;如果输出了,说明后续距离判断等逻辑有异常

修正后的完整脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class SelectableObject : MonoBehaviour
{
    NewInputActions inputActions;
    private float interInput;

    public GameObject player;
    public OutlineMaker outl;
    private bool isOutActive;

    private void Awake()
    {
        inputActions = new NewInputActions();
        inputActions.Other.Enable();
        inputActions.Other.Interact.performed += ctx => OnInteract();
    }

    private void OnDisable()
    {
        inputActions.Other.Interact.performed -= ctx => OnInteract();
        inputActions.Other.Disable();
    }

    void Update()
    {
        if (player == null) return; // 避免player为空时报错

        float dist = Vector3.Distance(player.transform.position, transform.position);
        if (dist < 5 && !isOutActive)
        {
            outl.EnableOutline();
            isOutActive = true;
        }
        else if (dist > 5 && isOutActive)
        {
            outl.DisableOutline();
            isOutActive = false;
        }
    }

    public void OnInteract()
    {
        Debug.LogWarning("DETECTED E"); // 优先验证触发状态
        if (player == null) return;

        float dist = Vector3.Distance(player.transform.position, transform.position);
        Debug.Log("距离判断结果:" + (dist > 5));
        
        // 注:这里逻辑疑似写反?距离大于5时隐藏对象是否符合需求?
        if (dist > 5)
        {
            gameObject.SetActive(false);
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 03:45:25