简体   繁体   English

Assets\\FPS\\Audio\\Soundtrack\\MusicManager.cs(35,70):错误 CS0841:在声明之前不能使用局部变量“currentScene”

[英]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:这些是我得到的错误:

  1. error CS0841: Cannot use local variable 'currentScene' before it is declared错误 CS0841:在声明之前不能使用局部变量“currentScene”
  2. The Type or namespace 'Scene' could not be found (are you missing a using directive or an 3. assembly reference?)找不到类型或命名空间“场景”(您是否缺少 using 指令或 3. 程序集引用?)
  3. The name 'SceneManager' does not exist in the current context当前上下文中不存在名称“SceneManager”
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));
  1. & 3. & 3。

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.

相关问题 声明之前无法使用局部变量 - Cannot use the local variable before it is declared SSIS —在声明局部变量“ If”之前不能使用 - SSIS — Cannot use local variable 'If' before it is declared 声明前不能使用局部变量DateTime吗? - Cannot use local variable DateTime before it is declared? 在声明之前不能使用局部变量 - Cannot use local variable before it is declared 错误 CS0165:使用未分配的局部变量“sot” - error CS0165: Use of unassigned local variable 'sot' 谁能帮我解决这个错误“Assets\PlayerMovment.cs(53,35): error CS1026: ) expected”? - Could anyone help me with this error "Assets\PlayerMovment.cs(53,35): error CS1026: ) expected"? 无法在此 scope 中声明名为“结果”的本地或参数(错误 CS0136) - A local or parameter named 'Result' cannot be declared in this scope (Error CS0136) 错误Assets / qwe.cs(22,35):错误CS1525:意外的符号` <internal> &#39;Unity3D - Error Assets/qwe.cs(22,35): error CS1525: Unexpected symbol `<internal>' Unity3D 在声明ASP.NET之前不能使用局部变量 - Cannot use local variable before it is declared ASP.NET 为什么会出现此错误:Assets\\Character2DController.cs(35,21): error CS0103: The name &#39;Physics2d&#39; does not exist in the current context - Why is this error appearing: Assets\Character2DController.cs(35,21): error CS0103: The name 'Physics2d' does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM