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

Unity3D答题游戏关卡显示问题:代码放置位置咨询

How to Add Level-Up Logic to Your Unity Quiz Game

Hey there! Let's get that level counter working properly. You're on the right track with your levelValue += 1 and UI update code—here's exactly where to place it, plus a few tweaks to make it persist across scene reloads (since you're reloading the scene after each question).

Step 1: Fix Level Persistence (Critical for Testing)

Right now, your levelValue resets to 0 every time the scene reloads because it's a local instance variable. To keep the level between scene loads, use PlayerPrefs to save and load the value. Update your Start method first:

void Start()
{
    // Load saved level value (default to 0 if no save exists)
    levelValue = PlayerPrefs.GetInt("CurrentLevel", 0);
    // Initialize the level UI immediately
    Level.text = "Level:" + levelValue.ToString();

    if (unansweredQuestions == null || unansweredQuestions.Count == 0)
    {
        unansweredQuestions = questions.ToList<Question>();
    }
    SetCurrentQuestion();
}

Step 2: Add the Level-Up Logic in the Right Place

Since you want to level up every time a question is completed (regardless of right/wrong for testing), you should trigger the level update right after the user submits their answer. To avoid repeating code, create a helper method first:

private void UpdatePlayerLevel()
{
    levelValue += 1;
    // Update the UI text (string interpolation is cleaner than concatenation)
    Level.text = $"Level: {levelValue}";
    // Save the new level to PlayerPrefs so it persists across scene reloads
    PlayerPrefs.SetInt("CurrentLevel", levelValue);
    PlayerPrefs.Save();
}

Now call this method in both your UserSelectTrue and UserSelectFalse methods, right before starting the transition coroutine:

public void UserSelectTrue ()
{
    animator.SetTrigger("True");
    if (currentQuestion.isTrue)
    {
        Debug.Log("CORRECT!");
    }else
    {
        Debug.Log("WRONG!");
    }
    // Trigger level up after answer is submitted
    UpdatePlayerLevel();
    StartCoroutine(TransitionToNextQuestion());
}

public void UserSelectFalse()
{
    animator.SetTrigger("False");
    if (!currentQuestion.isTrue)
    {
        Debug.Log("CORRECT!");
    }
    else
    {
        Debug.Log("WRONG!");
    }
    // Trigger level up after answer is submitted
    UpdatePlayerLevel();
    StartCoroutine(TransitionToNextQuestion());
}

Why This Works

  • The UpdatePlayerLevel method centralizes your level logic, making it easy to adjust later (like changing to only level up on correct answers).
  • Placing it after the answer validation ensures the level increments after the user completes the question, which aligns with your test requirement.
  • Using PlayerPrefs ensures the level doesn't reset when the scene reloads, so you can actually see the level increase as you complete questions.

Future Adjustments

When you're ready to refine the logic (e.g., only level up after 3 correct answers), just modify the UpdatePlayerLevel call: move it inside the Debug.Log("CORRECT!") blocks, or add a counter for correct answers that triggers the level up when it reaches a threshold.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:01:56