c#如何检测已破坏的克隆

时间:2018-12-06 07:49:12

标签: c# unity3d

我正在尝试在Unity上做一个烹饪模拟器游戏,当您单击生食盘时,x秒钟后它将在烤架上生成一个熟食克隆。如果我在x秒钟后未点击(销毁)该煮熟的食物,那么该煮熟的食物将成为燃烧的食物。

void OnMouseDown()
{
    // food item 1
    if (gameObject.name == "blobraw1") {
        StartCoroutine(cookandburn("blobraw1"));
    }
    else if (gameObject.name.Contains("blobcooked1")) {
        if(corder.SearchandRemove(gameObject)) {
            Destroy(gameObject);
        };
    }
}

public IEnumerator cookandburn(string rawFoodName) {
    float x = Random.Range(MinX, MaxX);
    float y = Random.Range(MinY, MaxY);

    yield return new WaitForSeconds(cooktime);
    if (rawFoodName == "blobraw1")
    {
        // generate raw food
        Instantiate(cooked1, new Vector3(x, y, 10), Quaternion.identity);
        yield return new WaitForSeconds(burntime);
        // generate burned food if condition doesn't satisfy
        Instantiate(burned1, new Vector3(x, y, 10), Quaternion.identity);
    }

当前,我可以销毁已煮熟的食物,但是x秒钟后,将运行刻录脚本并在确切位置启动食物的刻录版本。我想不出一种方法来检测煮熟的版本何时被破坏,并告诉程序不要创建已刻录版本的实例。

1 个答案:

答案 0 :(得分:0)

您需要添加isDestroyed = false标志。食物被破坏后,使其成为真实。实例化燃烧的食物之前,请检查条件。协程完成后,再次将标志设为假。如果要在其他脚本中使用,将标志设置为PlayerPrefs.SetInt("isDestroyed", 0)

可能会很方便
bool is Destroyed = false; //Define this on top of the script
public IEnumerator cookandburn(string rawFoodName) {
float x = Random.Range(MinX, MaxX);
float y = Random.Range(MinY, MaxY);

yield return new WaitForSeconds(cooktime);
if (rawFoodName == "blobraw1")
{
    // generate raw food
    Instantiate(cooked1, new Vector3(x, y, 10), Quaternion.identity);
    yield return new WaitForSeconds(burntime);
    // generate burned food if condition doesn't satisfy
    // Check that if the object was destroyed
    if(!isDestroyed){
        Instantiate(burned1, new Vector3(x, y, 10), Quaternion.identity);
    }
}
isDestroyed = false;
}