C# 一段时间后不出现的对象
因此,我将对象放在场景中,然后从检查器(对象名称旁边的复选框)将其设置为“不可见”(如果愿意,请停用),等待8秒钟后,它就不可见了。我使用的是Unity2D和C 我让游戏开始暂停三秒钟,然后再继续玩。第一个脚本就是那个脚本。该物品应该在8秒后重新出现,因此在游戏恢复后,这不起作用C# 一段时间后不出现的对象,c#,unity3d,flower,C#,Unity3d,Flower,因此,我将对象放在场景中,然后从检查器(对象名称旁边的复选框)将其设置为“不可见”(如果愿意,请停用),等待8秒钟后,它就不可见了。我使用的是Unity2D和C 我让游戏开始暂停三秒钟,然后再继续玩。第一个脚本就是那个脚本。该物品应该在8秒后重新出现,因此在游戏恢复后,这不起作用 //delay before level starts script using System.Collections; using System.Collections.Generic; using Unity
//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;
}
//级别启动脚本之前的延迟
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共课倒计时:单一行为
{
公共游戏对象倒计时;
私有void Start()
{
STARTCOUTROUTE(“StartDelay”);
}
无效更新()
{
}
IEnumerator StartDelay()
{
Time.timeScale=0;
float pauseTime=Time.realtimeSinceStartup+3f;
while(Time.realtimeSinceStartup
我还是不太明白你的两个方法叫Start
您只需在另一个协同例程结束时调用start例程
,就可以将它们链接在一起(尽管通常有更好的方法来实现您的目标):
我个人不喜欢协同程序。它们有时不太容易调试。我更喜欢用简单的计时器来做类似的事情(虽然在第一刻它看起来确实更糟)。优点是我现在可以直接在inspector中观看计时器倒计时:
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);
}
}
使用UnityEngine;
公共类SimpleCountDown:单一行为
{
[标题(“对象”)]
公共游戏对象倒计时对象;
公共游戏对象;
[标题(“设置”)]
//在这里您可以调整延迟时间
公共浮点数StartOffset=3;
公共浮点数=8;
[标题(“调试”)]
公共浮式起动器;
公众浮标计时器;
公共事业延误;
公共布尔延迟;
私有void Start()
{
startTimer=StartOffset;
flowerTimer=FlowerOffset;
isStartDelay=true;
}
私有void更新()
{
如果(!isStartDelay&!isFlowerDelay)返回;
如果(isStartDelay)
{
startTimer-=Time.deltaTime;
如果(startTimer)我刚刚尝试了你的代码,它可以工作。请确保你在inspector中禁用了游戏对象而不是它的精灵渲染器。顺便说一句,我不确定将Start()方法转换为分子是否是一个好主意,你可能应该制作一个单独的分子并在Start()中启动它功能。@ğatayikŞIK我的游戏开始暂停了。这可能会把它搞砸吗?我不确定一个非活动游戏对象上的脚本是否会注册到开始事件中。@Leo哦,我明白了。这可能是为什么它对其他人有效,而对我无效。是否可以在游戏中保持暂停的同时修复它?暂停只持续几秒钟ds.@Leo我应该提出一个新问题还是更新这个问题?嗯,什么意思这些都不起作用
?有错误吗?我测试了它们,它们都起作用了…你能告诉我你把组件连接到哪里了吗?当然,它可能不在FlowerObject上,也不在CountDownObject上!你引用了这两个对象吗?在第二个问题中,你能看到计时器在倒计时吗还是怎么了?
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);
}
}