简体   繁体   English

在C#Unity 3D中延迟负载级别

[英]Delaying load level in c# unity 3d

I have created some code that is meant to load my next level 20 seconds after the user has completed it, however i get an error "object reference is required to access a static function" however i need the function to be static to be able to call it when my helicopter doors open and the user can get in the helicopter. 我创建了一些代码,该代码打算在用户完成该操作后20秒钟加载我的下一个级别,但是我收到一条错误消息“需要对象引用才能访问静态函数”,但是我需要该函数是静态的才能当我的直升机门打开并且用户可以乘坐直升飞机时,请调用它。 Can anyone help? 有人可以帮忙吗?

    using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {


    static bool _hasPlayerWon = false;

    public static bool HasPlayerWon {get{return _hasPlayerWon;}}

    Health _playerHealth;




    public static void WinPlayer()
    {

        _hasPlayerWon = true;
        Invoke("loadSplash",20.0f);

    }


    void loadSplash ()
    {
        Application.LoadLevel("splash1");

    }


}

Create a field that holds a reference to the GameManager and call a Coroutine using that method that triggers the load. 创建一个包含对GameManager的引用的字段,并使用触发加载的方法调用协程。

Something like this: 像这样:

static private GameManager instance;

void Awake()
{
    instance = this;
}

public static void WinPlayer()
{
    _hasPlayerWon = true;
    instance.LoadLevel();
}

private void LoadLevel()
{
  StartCoroutine( LoadCo() );
}

private IEnumerator LoadCo()
{
  yield return new WaitForSeconds( 20.0 );

  Application.LoadLevel("splash1");
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM