[英]Assets\FPS\Audio\Soundtrack\MusicManager.cs(35,70): error CS0841: Cannot use local variable 'currentScene' before it is declared
I'm building this game, and I've been trying to fix a bug for very long, so I came here for some help...我正在构建这个游戏,并且我一直试图修复一个错误很长时间,所以我来这里寻求帮助......
These are the errors I'm getting:这些是我得到的错误:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManager;
public class MusicManager : MonoBehaviour
{
private AudioSource audiosource;
public AudioClip[] songs;
public float volume;
[SerializeField] private float songsPlayed;
[SerializeField] private bool[] beenPlayed;
List<string> DontChangeSongScenes = new List<string>();
// Start is called before the first frame update
void Start()
{
Scene currentScene = SceneManager.GetActiveScene();
audiosource = GetComponent<AudioSource>();
beenPlayed = new bool[songs.Length];
if (!audiosource.isPlaying || !DontChangeSongScenes.Contains(currentScene.name))
ChangeSong(Random.Range(0, songs.Length));
DontChangeSongScenes.Add("Scene_1"); /*just copy and paste this line and instead
of "Scene_1" write the name of the scene that shouldnt change the current playing song. */
DontChangeSongScenes.Add("Scene_2");
}
// Update is called once per frame
void Update()
{
audiosource.volume = volume;
if (!audiosource.isPlaying || !DontChangeSongScenes.Contains(currentScene.name))
ChangeSong(Random.Range(0, songs.Length));
if (songsPlayed == songs.Length)
{
songsPlayed = 0;
for (int i = 0; i < songs.Length; i++)
{
if (i == songs.Length)
break;
else
beenPlayed[i] = false;
}
}
}
public void ChangeSong(int songPicked)
{
if (!beenPlayed[songPicked])
{
songsPlayed++;
beenPlayed[songPicked] = true;
audiosource.clip = songs[songPicked];
audiosource.Play();
}
else
audiosource.Stop();
}
void Awake()
{
DontDestroyOnLoad(transform.gameObject);
}
}
The error is very clear.错误非常明显。 In your
Update
method you have not declared currentScene
.在您的
Update
方法中,您尚未声明currentScene
。 You could add it as a class field, or grab it each frame.您可以将其添加为类字段,或者每帧抓取它。
// Update is called once per frame
void Update()
{
Scene currentScene = SceneManager.GetActiveScene();
audiosource.volume = volume;
if (!audiosource.isPlaying || !DontChangeSongScenes.Contains(currentScene.name))
ChangeSong(Random.Range(0, songs.Length));
The namespace is SceneManagement
, not SceneManager
.命名空间是
SceneManagement
,而不是SceneManager
。
using UnityEngine.SceneManagement;
https://docs.unity3d.com/ScriptReference/SceneManagement.Scene.html https://docs.unity3d.com/ScriptReference/SceneManagement.Scene.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.