协同程序错误的行为时,场景加载 [英] Coroutine wrong Behavior when scene is loaded

查看:160
本文介绍了协同程序错误的行为时,场景加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我有这个协程:

Ok so I have this coroutine :

 IEnumerator ShowCharTraits()
    {

        while(!hasPlayerChosen)
        {
            yield return null;
            traitPanel.SetActive(true);
        }
        hasPlayerChosen = false;
        traitPanel.SetActive(false);
    //    Debug.Log("Got called! job done");

    }



它被称为像这样从清醒方法我游戏管理

players = GameObject.FindGameObjectsWithTag("Player");
                foreach (GameObject g in players)
                {
                    ui_Controller.StartShowCharTraits();
                    g.GetComponent<PlayerToken>().isTurn = false;
                }



StartShowCharTraits()是一个简便的方法,做这样的:

StartShowCharTraits() is a simple method that does this :

  public void StartShowCharTraits()
    {
        StartCoroutine("ShowCharTraits");
    }

现在,我已经检查了标签,没有空引用异常,居然没有错误或警告被抛出。如果我在编辑器中加载场景,然后发挥它的一切工作正常。 traitPanel.SetActive(真); 被调用,我的面板显示出来。然而,当我使用来自另一个场景载入我的场景 SceneManager.LoadScene(1); 上述行从未达到。任何想法,为什么发生这种情况?

Now, I have checked the tags, no null reference exception, actually no errors or warnings are being thrown. If i load the scene in the editor and then play it everything works fine. traitPanel.SetActive(true); get called and my panel shows up. However when I load my scene from another scene using SceneManager.LoadScene(1); the above mentioned line is never reached. Any ideas why this is happening ?

推荐答案

假设你想有一个集中的地方,就是像一个单身在团结项目。例如,

Say you want to have one central place that is "like a singleton" in a Unity project. Example,

SoundEffects
LapTimer
Scores
SocialMediaConnections

所有你要做的就是这一点。

All you do is this.


  1. 使你的脚本SoundEffects.cs

  1. make your script SoundEffects.cs

回忆一下每一个团结项目的必须预场景。 (这是不可能没有一个)

recall that every Unity project must have a "preload" scene. (it's impossible to not have one)

在预置场景中,有一个名为持有人一个空的游戏对象。请确保它被标记为DontDestroyOnLoad

in the preload scene, have a empty game object called "holder". make sure it is marked "DontDestroyOnLoad"

附加SoundEffects.cs到持有人

attach SoundEffects.cs to that holder

就大功告成了。

有只是没有更多的了。

你就完蛋了。

这只是简单的

所以,任何特定的脚本,恰好是连接到任何特定的对象,在任何特定的场景,可能需要访问SoundEffects...

So, any particular script, which happens to be attached to any particular object, in any particular scene, may need to access "SoundEffects"...

要做到这一点,只要做到这一点清醒:

To do so, simply do this in Awake:

 SoundEffects soundEffects = Object.FindObjectOfType<SoundEffects>();



然后就使用该脚本 soundEffects 的任何地方例如 soundEffects.PlayBoom​​(13)等等等等。

then just use soundEffects anywhere in that script, for example soundEffects.PlayBoom(13) etc etc.

因此,在清醒()

  Laps laps = Object.FindObjectOfType<Laps>();
  Social social = Object.FindObjectOfType<Social>();
  AI ai = Object.FindObjectOfType<AI>();

或什么的。

团结就是简单。的难以置信的简单。的这只是这么简单。

Unity is simple. Incredibly simple. It's just that easy.


  1. 写脚本, LapTimer.cs 或任何

把它放在支架上的对象在你的预紧场景(它还能在哪里呢?)

put it on a holder object in your preload scene (where else could it be?)

没有3,这是所有有。这只是可笑的简单。

there's no "3", that's all there is. It's just that ridiculously simple.

在统一初期,有人不幸提到的单身在QA板上的某个地方。 (你不能有单身在ECS系统,这是毫无意义的。)当时,这导致了如何可以使一个团结单身般啄,这是打桩混乱混乱巨大的讨论。不幸的是,从那天到这一点,让别人学习团结,谁看到在这里和那里提到的网单身,这导致了无休止的混乱。

In the early days of Unity, someone unfortunately mentioned "singletons" on a QA board somewhere. (You can't have "singletons" in an ECS system, it's meaningless.) At the time, this led to huge discussions about how you can make a "singleton-like thingy" in Unity, which is piling confusion on confusion. Unfortunately from that day to this, you get people learning Unity, who see "singleton" mentioned here and there on the net, and it leads to endless confusion.

再次指出,经理人的想法只是简单不可能在Unity - 如上所述。这是微不足道的。它只是再简单不过了。

请注意,上面我提到你可能想的句法糖果,以避免打字的令人难以置信的苦差事圈数圈= Object.FindObjectOfType<膝部>();

Note, above I mention you might want syntactic candy to avoid the incredible chore of typing Laps laps = Object.FindObjectOfType<Laps>();

有非常遗憾的是否宏在Unity / C#所以你需要另一种方式。

There are very unfortunately no macros in Unity/c# so you need another way.

所有你要做的就是使用网格脚本的 http://answers.unity3d.com/answers/1124859/view.html 它已经流行了多年的统一。

All you do is use a "Grid" script http://answers.unity3d.com/answers/1124859/view.html which has been popular for years in Unity.

但说实话,它是如此简单的使用分数成绩= Object.FindObjectOfType<比分>(); ,我通常只是做时下

But honestly, it's so simple to use Scores scores = Object.FindObjectOfType<Scores>(); that I usually just do that nowadays.

这篇关于协同程序错误的行为时,场景加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆