在非活动/停用的游戏对象上启动协程 [英] Start coroutine on an inactive/de-activated GameObject

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

问题描述

我有以下代码:

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!

这是有道理的,因为在运行脚本之前游戏对象已被设置为停用.即便如此,那我该怎么做呢?在 WaitForSeconds() 之前,我尝试将 gameObject.SetActive(false) 移动到协程.这样做完全停止了游戏对象的加载.

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 类下的一个函数.当您必须在停用的游戏对象上启动协程时,您需要引用具有活动游戏对象的 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.使用不太可能被停用的现有游戏对象.就我而言,我通常使用相机.我访问相机的 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.将脚本附加到一个空游戏对象上,空游戏对象上的脚本将控制或能够激活/取消激活另一个游戏对象.


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.

您希望在已取消激活的游戏对象上运行具有协程功能的脚本(将其附加到您希望取消激活的游戏对象):

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 脚本,但仍然能够启动它的 加载协程函数,用新脚本创建一个空的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.

创建一个空的游戏对象,然后将此脚本附加到它:

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.

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

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