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

Unity报错CS0122:SceneLoader方法访问权限问题及解决咨询

Fixing CS0122 Error: Accessing Private Method in Unity

Hey there, let's tackle this error and get your win condition working properly!

What the Error Means

The error CS0122: 'SceneLoader.LoadNextScene()' is inaccessible due to its protection level is telling you exactly what's wrong: your GameController script is trying to call the LoadNextScene() method from SceneLoader, but that method is marked as private. Private methods can only be used inside the class they're defined in—external scripts like GameController can't access them directly.

Fix 1: Make LoadNextScene() Public

The simplest fix is to change the access modifier of LoadNextScene() from private to public in your SceneLoader script. This lets other scripts call it freely:

using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour {
    [SerializeField] float levelLoadDelay = 2f;
    private int currentSceneIndex = -1;

    private void Awake() {
        currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    }

    private void Start() {
        if (currentSceneIndex == 0) {
            Invoke("LoadFirstScene", 2f);
        }
    }

    // Changed from private to public here
    public void LoadNextScene() {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        int nextSceneIndex = currentSceneIndex + 1;
        SceneManager.LoadScene(nextSceneIndex);
    }

    public void LoadFirstScene() {
        SceneManager.LoadScene(1);
    }
}

Fix 2: Use a Public Wrapper (If You Want to Keep LoadNextScene() Private)

If you prefer to keep LoadNextScene() private (maybe to enforce internal logic), you can add a public "wrapper" method in SceneLoader that triggers it:

// Add this public method to SceneLoader
public void TriggerWinSceneLoad() {
    LoadNextScene(); // Calls the private method internally
}

Then update your GameController's WinGame() method to call this wrapper instead:

public void WinGame() {
    // Fix your win condition logic first (see note below!)
    allEnemiesKilled = enemiesKilled >= enemiesInScene;
    if (allEnemiesKilled) {
        sceneLoader.TriggerWinSceneLoad();
    }
}

Important Logic Fix for Your Win Condition

I noticed a bug in your WinGame() method—right now you're setting allEnemiesKilled = true when fewer enemies are killed than exist in the scene, which is backwards! You should only trigger the win if all enemies are dead:

public void WinGame() {
    // Correctly check if all enemies are killed
    allEnemiesKilled = enemiesKilled >= enemiesInScene;
    
    // Only load the next scene if the win condition is met
    if (allEnemiesKilled) {
        sceneLoader.LoadNextScene(); // or sceneLoader.TriggerWinSceneLoad() if using Fix 2
    }
}

Final Checks

  1. Make sure your scenes are added to the Build Settings (File > Build Settings) so SceneManager.LoadScene() can find them.
  2. Verify that enemiesKilled is being incremented correctly when enemies are destroyed (you'll need code in your Enemy script to call GameController.AddScore() and increment enemiesKilled).

Once you apply these changes, your game should properly load the next scene when all enemies are eliminated!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:33:24