在无效/停用的GameObject上启动协程 [英] Start coroutine on an inactive/de-activated GameObject

查看:932
本文介绍了在无效/停用的GameObject上启动协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

void Start()
{
    gameObject.SetActive(false);
    StartCoroutine(Load());
}

IEnumerator Load()
{
    yield return new WaitForSeconds(waitTime);
    gameObject.SetActive(true);
}

这给了我一个错误,提示:

This gives me an error that says:

协程无法启动,因为游戏对象"NameOfObj" 不活跃!

Coroutine couldn't be started because the the game object 'NameOfObj' is inactive!

这很有意义,因为在运行脚本之前已将游戏对象设置为停用.即使如此,我该怎么办呢?我试过将gameObject.SetActive(false)移至协程,而不是WaitForSeconds().这样做完全停止了游戏对象的加载.

This makes sense, since the game object has been set to deactivate before running the script. Even still, what is it that I'm supposed to do then? I tried moving gameObject.SetActive(false) to the coroutine, before WaitForSeconds(). Doing this stopped the game object from loading at all.

据我了解,执行gameObject.SetActive(false)行时,脚本将停止运行,直到重新激活游戏对象.但是,如果是这种情况,那么重新激活游戏对象(因为禁用了脚本)不是不可能吗?

From my understanding, when the line gameObject.SetActive(false) is executed, the script stops running until the game object is reactivated. However, if this is the case, would it not be impossible to then reactivate the game object (as the script is disabled)?

无论如何,在游戏开始后我如何将我的游戏对象的加载延迟到2-3(或任意时间长度)?

Regardless, how would I delay my game object from loading until 2-3 (or any arbitrary length of time) after the game has started?

推荐答案

您不能从已禁用其GameObject的脚本中启动协程函数.

You cannot start a coroutine function from a script that has its GameObject de-activated.

StartCoroutine函数是MonoBehaviour类下的函数.当您必须在已停用的GameObject上启动协程时,您需要引用具有活动GameObject的MonoBehaviour对象.

The StartCoroutine function is a function under the MonoBehaviour class. When you have to start a coroutine on a deactivated GameObject, you need a reference to a MonoBehaviour object that has an active GameObject.

两种方法:

1 .使用不太可能被停用的现有GameObject.就我而言,我通常使用相机.我访问了相机的MonoBehaviour,因为它可能已被激活,然后使用它来启动协同程序功能.

1. Use an already existing GameObject that's unlikely to be deactivated. In my case, I usually use the camera. I access the camera's MonoBehaviour since it's likely to be activated then use it to start the coroutine function.

我建议您使用此方法.

用以下代码替换Start函数中的代码:

Replace the code in your Start function with the one below:

//De-activate this GameObject
gameObject.SetActive(false);
//Get camera's MonoBehaviour
MonoBehaviour camMono = Camera.main.GetComponent<MonoBehaviour>();
//Use it to start your coroutine function
camMono.StartCoroutine(Load());


2 .将脚本附加到一个空的GameObject上,该空GameObject上的脚本将控制或能够激活/停用另一个GameObject.


2. Attach the script to an empty GameObject and the script on the empty GameObject will control or be able to activate/de-activate the other GameObject.

带有协程函数的脚本,您希望在停用的GameObject上运行(将其附加到要停用的GameObject上):

The script with the coroutine function you expect to run on a de-activated GameObject (Attach it to the GameObject you wish to de-activate):

public class YourDeactivatableScript: MonoBehaviour
{
    public IEnumerator Load()
    {
        yield return new WaitForSeconds(waitTime);
        gameObject.SetActive(true);
    }
}

现在,假设您要停用名为"Cube" 的游戏对象,该游戏对象已附加YourDeactivatableScript脚本,但仍能够启动其Load协程功能,请创建一个用新脚本清空GameObject,然后从中启动Load功能.

Now, let's say that you want to deactivate a GameObject named "Cube" that has the YourDeactivatableScript script attached to it but still be able to start its Load coroutine function, create an empty GameObject with a new script, then start the Load function from it.

创建一个空的GameObject,然后将此脚本附加到它:

Create an empty GameObject then attach this script to it:

public class LoadFuncCallerScript: MonoBehaviour
{
    GameObject targetObject;

    public void Start()
    {
         //Find the GameObject you want to de-activate
        targetObject = GameObject.Find("Cube");
        //De-activate it
        targetObject.SetActive(false);
        //Get it's component/script
        YourDeactivatableScript script = targetObject.GetComponent<YourDeactivatableScript>();
        //Start coroutine on the other script with this MonoBehaviour
        StartCoroutine(script.Load());
    }
}

协程现在从另一个名为LoadFuncCallerScript的脚本启动.

The coroutine is now started from another script named LoadFuncCallerScript.

这篇关于在无效/停用的GameObject上启动协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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