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

Unity中C#按键输入无响应问题排查求助

Troubleshooting Input Issues in Your Unity Text Game

Alright, let's dig into why your key presses aren't triggering scene switches. I’ve looked over your code and spotted a few potential issues, plus some standard Unity checks to run through step by step:

1. Fix the RenderStory() Call Timing in Update()

Right now, you’re calling RenderStory() every single frame at the end of Update(). That’s problematic because:

  • As soon as you press a key to set sNextHeading, the same frame’s RenderStory() will clear sNextHeading immediately. While this might not break things outright, it’s unnecessary and can lead to unexpected behavior.
  • Instead, call RenderStory() only right after you modify sNextHeading (inside each key press condition). This ensures the scene switch logic runs exactly when it needs to.

2. Check for Mismatched Heading Names (Critical Bug!)

I noticed a clear typo in your key logic that’s definitely breaking part of your flow, and it might be contributing to the initial issue too:
In your KeyCode.P check, you’re looking for sCurrHeading == "Bowl", but your Page object’s heading is actually "Check Bowl". That means when you navigate to the bowl scene, pressing P will do nothing because the string doesn’t match.

Fix that line to:

if (sCurrHeading == "Check Bowl") {
    sNextHeading = "Pick Up";
}

3. Verify sCurrHeading Gets Initialized Correctly

Your Start() method calls RenderStory() to set sCurrHeading to "Wake Up", but let’s confirm this is actually working. Add a debug log inside RenderStory() when it updates sCurrHeading:

void RenderStory() {
    if (!string.IsNullOrEmpty(sNextHeading)) {
        for (int nPage = 0; nPage < aBook.Length; nPage++) {
            if (sNextHeading == aBook[nPage].Heading) {
                sCurrHeading = sNextHeading;
                sNextHeading = "";
                Debug.Log($"Current heading updated to: {sCurrHeading}"); // Add this log
                Debug.Log(aBook[nPage].Body);
            }
        }
    }
}

If you don’t see this log when the game starts, that means BindBook() isn’t creating the Page array correctly, or there’s a typo in the "Wake Up" heading string.

4. Confirm Script and GameObject Activation

  • Make sure this script is attached to a GameObject that’s active in the scene (not disabled in the Hierarchy).
  • Double-check that the script’s enabled checkbox is ticked in the Inspector.
  • Ensure no other scripts in the scene are interfering with input (like calling Input.ResetInputAxes() or capturing key presses before this script runs).

5. Check Unity’s Input Settings

  • Go to Edit > Project Settings > Input Manager and confirm keys like W, D, B aren’t remapped or conflicting with other input axes.
  • If you’re using Unity’s newer Input System (2021+), make sure you haven’t mixed it with the old Input Manager. The Input.GetKeyDown() API only works with the legacy system—if you’ve enabled the new Input System, you’ll need to use its API instead.

6. Debug Key Press Triggers

To confirm if key presses are even being detected, add debug logs inside each key condition. For example:

if (Input.GetKeyDown(KeyCode.W)) {
    if (sCurrHeading == "Wake Up") {
        sNextHeading = "Check Woven Basket";
        Debug.Log("W key pressed - switching to Woven Basket scene");
        RenderStory(); // Move the call here
    }
}

If you don’t see this log when pressing W, the issue is with input detection (go back to steps 4 and 5). If you do see it but the scene doesn’t switch, then RenderStory() isn’t processing the sNextHeading change correctly.

Start with these steps—most likely the combination of the heading typo and RenderStory() timing is causing your issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:38:38