简体   繁体   English

UnityException:不允许从 MonoBehaviour 构造函数调用 GetActiveScene

[英]UnityException: GetActiveScene is not allowed to be called from a MonoBehaviour constructor

I'm trying to make a save level system and I keep on getting this error.我正在尝试制作一个保存级别系统,但我不断收到此错误。

UnityException: GetActiveScene is not allowed to be called from a MonoBehaviour constructor UnityException:不允许从 MonoBehaviour 构造函数调用 GetActiveScene

I've tried searching this up, but there were no results.我试过搜索这个,但没有结果。 Here is the code I used:这是我使用的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class EndLevel : MonoBehaviour
{
    public PlayerMovement pm;

    public GameObject completeLevelUI;

    // Start is called before the first frame update
    void Start() { 
    }

    // Update is called once per frame
    void Update() {
    }

    void OnCollisionEnter (Collision collisionInfo) {
        if(collisionInfo.collider.tag == "Finish") {
            Debug.Log("You beat the level!");
            pm.enabled = false;
            completeLevelUI.SetActive(true);
            Level = Level + 1;
            PlayerPrefs.SetFloat("Level", Level);
            Debug.Log("Saved");
            Invoke("NextLevel", 3);
        }
    }

    public void NextLevel() {
        SceneManager.LoadScene (SceneManager
            .GetActiveScene().buildIndex + 1);
    }
}

Any Ideas about the error?关于错误的任何想法?

You have to get the current active Scene in the Start() or Awake() methods.您必须在Start()Awake()方法中获取当前活动场景。

Example with Start() : Start()示例:

private int sceneNumber;

private void Start() {
    sceneNumber = SceneManager.GetActiveScene().buildIndex;
}

As an alternative solution you could also use a Lazzy Getter.作为替代解决方案,您还可以使用 Lazzy Getter。 Which means that the value won't get stale between when the scene loads and when you use it, which might matter with other pieces of data.这意味着该值在场景加载和使用之间不会过时,这可能与其他数据片段有关。

Example with Lazzy Getter:使用 Lazzy Getter 的示例:

private int sceneNumber { 
    get { 
        return SceneManager.GetActiveScene().buildIndex; 
    }
}

When you use either of those solutions, you can now simply call scenenumber + 1 in your SceneManager.Load() function call instead.当您使用这些解决方案中的任何一个时,您现在只需在SceneManager.Load()函数调用中调用scenenumber + 1即可。

Additionally you need to ensure that you are calling an IEnumerator , instead of Invoking the Function call.此外,您需要确保您正在调用IEnumerator ,而不是调用函数调用。 If you want to delay the scene loading.如果你想延迟场景加载。

Function Call:函数调用:

private void OnCollisionEnter (Collision collisionInfo) {
    ...
    StartCoroutine(NextLevel(3f));
    ...
}

private IEnumerator NextLevel(float seconds) {
    yield return new WaitForSeconds(seconds);
    SceneManager.LoadScene(sceneNumber + 1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnityException:不允许从 MonoBehaviour 构造函数调用 get_time - UnityException: get_time is not allowed to be called from MonoBehaviour constructor C# - 不允许从 MonoBehaviour 构造函数调用 RandomRangeInt - C# - RandomRangeInt is not allowed to be called from a MonoBehaviour constructor “不允许从 MonoBehaviour 构造函数调用加载”在不从 MonoBehaviour 继承的脚本上 - "Load is not allowed to be called from MonoBehaviour constructor" On script that does not inherit from MonoBehaviour 不允许从 Monobehaviour 调用 persistentDatapath - persistentDatapath is not allowed called from Monobehaviour UnityException:不允许调用此函数 - UnityException: Not allowed to call this function 统一错误(脚本检查器3):不允许从ScriptableObject构造函数调用GetBool - Error in unity(Script Inspector 3): GetBool is not allowed to be called from a ScriptableObject constructor 错误帮助-UnityException:声明变量时不允许调用此函数 - Error Help - UnityException: You are not allowed to call this function when declaring a variable 为什么在非MonoBehaviour类中进行多个构造函数调用? - Why multiple constructor calls in a non MonoBehaviour class? 实例化从MonoBehaviour派生的类 - Instantiate a class that derives from MonoBehaviour 我可以从非单一行为脚本中访问单一行为脚本吗? - Can i access a monobehaviour script from within a non monobehaviour script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM