简体   繁体   English

延迟后统一重生

[英]unity respawn after delay

I'm trying to respawn an object at "x" amount of time however when i use yield and startcourutine it causes a lot of errors and mistakes in my code. 我正在尝试在“ x”时间重新生成对象,但是当我使用yield和startcourutine时,它会在我的代码中引起很多错误和错误。 and instead of destroying i want to hide the object and enable it again when i respawn it after the delay. 而不是销毁我想隐藏该对象并在延迟后重新生成它时再次启用它。 how is this possible if i have arrays? 如果我有数组怎么办?

For a direct respawn on the same position you can do this: 要在相同位置直接重生,可以执行以下操作:

public class CrateCrash : MonoBehaviour
{
    ...

    public void InitRespawn(GameObject toRespawn)
    {
        StartCoroutine(RespawnObject(toRespawn, 5.0f));
    }

    private IEnumerator RespawnObject(GameObject toRespawn, float delay)
    {
        yield return new WaitForSeconds(delay);
        toRespawn.SetActive(true);
    }
}

The other class: 另一类:

using UnityEngine;
using System.Collections;


public class Crate : MonoBehaviour
{   
    CrateCrash manager;    

    public void OnMouseDown()
    { 
        manager.InitRespawn(gameObject);

        gameObject.SetActive(true);

    }

    public void SetManagerReference(CrateCrash managerRef)
    {
        manager = managerRef;
    }    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM