如果所有敌人都被杀死并赢得了比赛,我试图让玩家飞船的游戏对象移到下一个场景。我在Unity控制台中看到一条错误消息:
Assets \ Scripts \ GameController.cs(57,25):错误CS0122:“ SceneLoader.LoadNextScene()”由于其保护级别而无法访问。
当前,我只能在更改此行的值时转到下一个场景:
SceneManager.LoadScene(1);
到场景Loader.cs中的(2),我在这里在游戏控制器中添加了代码:public void WinGame()方法,该错误的含义以及如何解决的任何反馈都值得赞赏。 :)
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);
}
}
答案 0 :(得分:0)
使用当前的代码结构,问题是您在start函数中定义了方法。
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);
}
您现在可以使用以下代码调用LoadNextScene:
SceneLoader.instance.LoadNextScene();