首页 > 解决方案 > 对象在一定时间后未出现

问题描述

因此,我将对象放入场景中,然后通过检查器(对象名称旁边的复选标记框)将其设置为“不可见”(如果您愿意,请停用),等待 8 秒后它不可见。我正在使用 Unity 2d 和 C#。

我让游戏开始暂停三秒钟,然后在有效之后播放。第一个脚本就是那个。该项目应该在 8 秒后重新出现,所以在游戏恢复后,这不起作用。

  //delay before level starts script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class countDown : MonoBehaviour
{

public GameObject CountDown;


  private void Start()
 {
    StartCoroutine("StartDelay");

  }

  void Update()
 {

    }
  IEnumerator StartDelay()
  {
     Time.timeScale = 0;
     float pauseTime = Time.realtimeSinceStartup + 3f;
      while (Time.realtimeSinceStartup < pauseTime)
         yield return 0;
     CountDown.gameObject.SetActive(false);
     Time.timeScale = 1;
 }

 {




//script for the flower to appear
 IEnumerator Start()
  {
    print(Time.time);
    yield return new WaitForSeconds(8);
    print(Time.time);
    flowerInScene.gameObject.SetActive(true);
  }

  [SerializeField] Transform flowerInScene;
 }

标签: c#unity3dflower

解决方案


我仍然没有真正得到你的两种方法Start

你可以简单地StartCoroutine在另一个协程的末尾调用 a ,这样你就可以将它们链接在一起(尽管通常有更好的方法来做你想做的事情):

using System.Collections;
using UnityEngine;

public class CountDown : MonoBehaviour
{
    public GameObject CountDownObject;
    public GameObject flowerObject;

    private void Start()
    {
        StartCoroutine(Delay());
    }

    private IEnumerator Delay()
    {
        yield return new WaitForSeconds(3);
        HideCountdown();
        StartCoroutine(FlowerDelay());
    }

    private void HideCountdown()
    {
        CountDownObject.SetActive(false);
    }

    private IEnumerator FlowerDelay()
    {
        yield return new WaitForSeconds(8);
        ShowFlower();
    }

    private void ShowFlower()
    {
        flowerObject.SetActive(true);
    }
}

我个人不喜欢 Coroutines .. 它们有时并不那么容易调试。我更喜欢用简单的计时器做这样的事情(尽管在第一刻它看起来确实更糟)。优点是我现在可以直接在检查器中观看计时器倒计时:

using UnityEngine;

public class SimpleCountDown : MonoBehaviour
{
    [Header("The Objects")]
    public GameObject CountDownObject;
    public GameObject FlowerObject;

    [Header("Settings")]
    // Here you can adjust the delay times
    public float StartOffset = 3;
    public float FlowerOffset = 8;

    [Header("Debug")]
    public float startTimer;
    public float flowerTimer;

    public bool isStartDelay;
    public bool isFlowerDelay;

    private void Start()
    {
        startTimer = StartOffset;
        flowerTimer = FlowerOffset;
        isStartDelay = true;
    }

    private void Update()
    {
        if (!isStartDelay && !isFlowerDelay) return;

        if (isStartDelay)
        {
            startTimer -= Time.deltaTime;
            if (startTimer <= 0)
            {
                HideCountdown();
                isStartDelay = false;
                isFlowerDelay = true;
            }
        }

        if (isFlowerDelay)
        {
            flowerTimer -= Time.deltaTime;
            if (flowerTimer <= 0)
            {
                ShowFlower();
                isFlowerDelay = false;
                this.enabled = false;
            }
        }
    }

    private void HideCountdown()
    {
        CountDownObject.SetActive(false);
    }

    private void ShowFlower()
    {
        FlowerObject.SetActive(true);
    }
}

推荐阅读