-3

I tried to get the player ship game object to move to the next scene if all enemies are killed and won the game. I get an error in the Unity console says:

Assets\Scripts\GameController.cs(57,25): error CS0122: 'SceneLoader.LoadNextScene()' is inaccessible due to its protection level.

Currently, I can only go to the next scene when I change the value in the of this line:

SceneManager.LoadScene(1); 

to (2) in the scene Loader.cs I added code to the game controller this here: the public void WinGame() method any feedback to what this error s means and how I can fix it is appreciated. :)

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);
        }

        private void LoadNextScene()
        {
            int currentSceneIndex =   SceneManager.GetActiveScene().buildIndex;
            int nextSceneIndex = currentSceneIndex + 1;
            SceneManager.LoadScene(nextSceneIndex);
        }

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


using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.UI;

public class GameController : MonoBehaviour
{
    public Text scoreText;
    public Text restartText;
    public Text gameOverText;

    [SerializeField] private Text outcome;
    [SerializeField] private PlayableDirector masterTimelinePlayableDirector;
    [SerializeField] private GameObject playerShip;

    private bool winGame = false;
    private bool gameOver = false;
    private int score;
    private int enemiesLeft = 0;
    private SceneLoader sceneLoader;

    public bool allEnemiesKilled;
    public float enemiesInScene = 10.0f;
    public float enemiesKilled;

    void Start()
    {
        sceneLoader = FindObjectOfType<SceneLoader>();
        enemiesLeft = GameObject.FindObjectsOfType<Enemy>().Length;
        restartText.text = "";
        gameOverText.text = "";
        outcome.text = "";
        score = 0;
        UpdateScore();
    }

    void Update()
    {
        if (gameOver)
        {
            if (Input.GetKeyDown(KeyCode.R))
            {
                 sceneLoader.LoadFirstScene();
            }
        }

        if (PlayerHealth.cur_health <= 0)
        {
            GameOver();
        }
    }

    public void WinGame()
    {
        if (enemiesKilled < enemiesInScene)
        {
             allEnemiesKilled = true;
        }

        sceneLoader.LoadNextScene();
    }

    public void DecreaseEnemyCount()
    {
        enemiesLeft--;
    }

    public void AddScore(int newScoreValue)
    {
        score += newScoreValue;
        UpdateScore();
    }

    void UpdateScore()
    {
        scoreText.text = "Score: " + score;
    }

   public void GameOver()
   {
        gameOver = true;
        gameOverText.text = "Game Over!";

        if (enemiesLeft > 0)
        {
             outcome.text = "You Lose";
        }
        else
        {
            outcome.text = "You Win";
        }

        restartText.text = "Press 'R' for Restart";

        masterTimelinePlayableDirector.Stop();
        playerShip.SetActive(false);
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
martin
  • 45
  • 6
  • Change it to public. – Cid Jun 19 '19 at 12:32
  • 3
    Possible duplicate of [In C#, what is the difference between public, private, protected, and having no access modifier?](https://stackoverflow.com/questions/614818/in-c-what-is-the-difference-between-public-private-protected-and-having-no) – Cid Jun 19 '19 at 12:33
  • 1
    I'm sure you would have a bunch of solutions typing `is inaccessible due to its protection level` in your favorite search engine – Cid Jun 19 '19 at 12:34
  • I didn't find my issue with typing this any guidance is much appreciated .. – martin Jun 19 '19 at 12:42
  • You still didnt find the solution? Make your `LoadNextScene()` function public in `SceneLoader script`. – Saad Anees Jun 19 '19 at 12:51
  • You can access `private` members **only** from the same class, and `protected` members from that class and its inheritors. Make it `public` to access from outside. – trollingchar Jun 19 '19 at 12:53
  • Thank you for the first comment it removed the error. still can't go to next scene I update code in-game controller and in scene loader please look over once again. Thank you – martin Jun 19 '19 at 12:56
  • Don't fix your code in your question. You edited and vandalized your own question. now, the methods are inside the `Start()` method – Cid Jun 19 '19 at 13:30
  • Your LoadNextScene is only a function available in Start, outside that.. its toast. – BugFinder Jun 19 '19 at 13:31
  • @BugFinder I will look into it Thank you – martin Jun 19 '19 at 14:57
  • 1
    @BugFinder No, this wasn't the case when OP asked the question. Check the edit history, he vandalized his own question – Cid Jun 19 '19 at 15:18

1 Answers1

0

With your current code structure, the problem is that you have method defined inside the start function.

Modified script:

using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
[SerializeField] float levelLoadDelay = 2f;
private int currentSceneIndex = -1;
 public static SceneLoader instance;

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

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

    public void LoadNextScene()
    {
        int currentSceneIndex =   SceneManager.GetActiveScene().buildIndex;
        int nextSceneIndex = currentSceneIndex + 1;
        SceneManager.LoadScene(nextSceneIndex);
    }

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

You can now call the LoadNextScene by using the code:

SceneLoader.instance.LoadNextScene();
Matthew
  • 1,905
  • 3
  • 19
  • 26