Unity Collision删除然后添加回来

sha*_*ane 2 c# enums triggers collision-detection unity-game-engine

我有对象滚动屏幕,无休止地这样做.我想在触摸时移除对象(我得到那个部分)SetActive/Destroy.
我正在尝试将对象添加回场景,以便可以重复使用它们.

我该怎么做?

if (col.gameObject.tag == "Coin"){
        coinCount++;
        coins.text = coinCount.ToString();

        // Save Coins
        PlayerPrefs.SetInt("Coins", coinCount);
        col.gameObject.SetActive(false);
    }
Run Code Online (Sandbox Code Playgroud)

上面的代码在 - > OnTriggerEnter2D(Collider2D){}

// Waits Seconds to put coins in correct spot
IEnumerator afterSeconds(GameObject x)
{
    //x.SetActive(false);

    yield return new WaitForSeconds(5f);//5f

    //x.SetActive(true);
}
Run Code Online (Sandbox Code Playgroud)

Eas*_*ier 5

您可以使用MonoBehavior.Invoke()并将类变量设置为对撞机,但如果在重新启用步骤之前发生冲突,则会发生混乱.相反,我会使用Coroutine.

if (col.gameObject.tag == "Coin"){
        coinCount++;
        coins.text = coinCount.ToString();

        // Save Coins
        PlayerPrefs.SetInt("Coins", coinCount);
        StartCoroutine(afterSeconds(col.gameObject)); //hide, delay, show  
    }
Run Code Online (Sandbox Code Playgroud)

然后:

// Waits Seconds to put coins in correct spot
IEnumerator afterSeconds(GameObject x)
{
    x.SetActive(false);
    yield return new WaitForSeconds(5f);
    x.SetActive(true);
}
Run Code Online (Sandbox Code Playgroud)