Unity报错CS0122:SceneLoader方法访问权限问题及解决咨询
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
- Make sure your scenes are added to the Build Settings (File > Build Settings) so
SceneManager.LoadScene()can find them. - Verify that
enemiesKilledis being incremented correctly when enemies are destroyed (you'll need code in your Enemy script to callGameController.AddScore()and incrementenemiesKilled).
Once you apply these changes, your game should properly load the next scene when all enemies are eliminated!
内容的提问来源于stack exchange,提问作者martin

