不能将 WaitForSeconds 与 OnMouseDown 一起使用

本文关键字:一起 OnMouseDown WaitForSeconds 不能 | 更新日期: 2023-09-27 18:37:24

我用OnMouseDown()来停用一个对象,但我希望该对象在几秒钟内再次激活。我已经将WaitForSeconds()用于其他事情,但这次它不起作用

这是我可以通过研究收集的(停用部分工作正常):

void Start()
{
    StartCoroutine(wait());
}
void Update(){}
void OnMouseDown()
{
    gameObject.SetActive(false);
}
IEnumarator wait()
{
    yield return new WaitForSeconds(3);
    gameObject.SetActive(true);
}

不能将 WaitForSeconds 与 OnMouseDown 一起使用

您的代码无法正常工作的原因太多了。你是在倒退。当程序启动时,您的协程立即启动,因为wait()是从 Start() 函数调用的。 启动时,它会暂停 3 秒钟,并将游戏对象设置为 SetActive(true) ;

如果您的游戏对象已在屏幕上可见,则您的代码不会执行任何操作,因为即使SetActive(true)可见也会调用它。 如果您在 3 秒之前未能按下/单击屏幕,您将无法看到SetActive(true);因为那时您的协程代码已经完成运行。

此外,如果禁用游戏对象,附加到该游戏对象的协程将停止。解决方案是创建要禁用的游戏对象的引用,然后使用该引用另一个脚本禁用启用,而不会出现问题。

由于提供了代码,我为您修复/重写了它。我用更强大的功能替换了OnMouseDown功能。

您所要做的就是创建一个空的游戏对象。将此脚本附加到该空游戏对象。然后从编辑器中将要禁用启用的游戏对象拖放到此脚本中的"要禁用的游戏对象"插槽中。

请勿将此脚本附加到要禁用启用的游戏对象

用立方体测试,它有效。

using UnityEngine;
using System.Collections;
public class ALITEST: MonoBehaviour
{
    //Reference to the GameObject you want to Disable/Enable
    //Drag the Object you want to disable here(From the Editor)
    public GameObject gameObjectToDisable;
    void Start()
    {
    }
    void Update()
    {
        //Keep checking if mouse is pressed
        checkMouseClick();
    }
    //Code that checks when the mouse is pressed down(Replaces OnMouseDown function)
    void checkMouseClick()
    {
        //Check if mouse button is pressed
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hitInfo = new RaycastHit();
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo))
            {
                //Check if the object clicked is that object
                if (hitInfo.collider.gameObject == gameObjectToDisable)
                {
                    Debug.Log("Cube hit");
                    StartCoroutine(wait()); //Call the function to Enable/Disable stuff
                }
            }
        }
    }
    //This value is used to make sure that the coroutine is not called again while is it already running(fixes many bugs too)
    private bool isRunning = false;
    IEnumerator wait(float secondsToWait = 3)
    {
        //Exit coroutine while it is already running
        if (isRunning)
        {
            yield break; //Exit
        }
        isRunning = true;
        //Exit coroutine if gameObjectToDisable is not assigned/null
        if (gameObjectToDisable == null)
        {
            Debug.Log("GAME OBJECT NOT ATTACHED");
            isRunning = false;
            yield break; //Exit
        }
        gameObjectToDisable.SetActive(false);
        //Wait for x amount of Seconds
        yield return new WaitForSeconds(secondsToWait);
        //Exit coroutine if gameObjectToDisable is not assigned/null
        if (gameObjectToDisable == null)
        {
            Debug.Log("GAME OBJECT NOT ATTACHED");
            isRunning = false;
            yield break; //Exit
        }
        gameObjectToDisable.SetActive(true);
        isRunning = false;
    }
}

因为您在 Start() 中调用 StartCoroutine(),所以您的协程将在组件启动 3 秒后恢复。您希望在OnMouseDown()中调用StartCoroutine(wait()),以便游戏对象在此之后变为活动状态。

您无法停用GameObject并继续Coroutine。如果停用正在运行Coroutine GameObject,它将停止。

因此,如果您想正确执行此操作,则需要在其他GameObject上运行Coroutine然后从那里激活此GameObject

如果您需要更多帮助,请询问。