Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
712 views
in Technique[技术] by (71.8m points)

c# - Coroutine wrong Behavior when scene is loaded

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");

    }

It's being called like this from the awake method in my GameManager:

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

StartShowCharTraits() is a simple method that does this :

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

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. make your script SoundEffects.cs

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

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

  4. attach SoundEffects.cs to that holder

you're done.

there's just nothing more to it.

you're finished.

it's "just that simple"

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>();

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

So in Awake()

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

or whatever.

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

  1. write your script, LapTimer.cs or whatever

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

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

Note that...

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.

Once again, note that the idea of "managers" is just impossibly simple in Unity - explained above. It's trivial. It just couldn't be easier.


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

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...