【发布时间】:2021-01-05 12:19:14
【问题描述】:
我写了一个代码,当播放器没有在相机中渲染时,它应该被销毁,但即使在相机中渲染它也会被销毁,请参阅我下面的代码;
using UnityEngine;
public class IfnotvisibleDestroy : MonoBehaviour
{
public SpriteRenderer re;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
public void Update()
{
if (re.isVisible)
{
Debug.Log(re.isVisible);
}
if(!re.isVisible)
{
Destroy(gameObject);
}
}
}
【问题讨论】:
-
您确定对于该组件和渲染器的同一实例,日志和销毁发生在同一帧中吗?如果你宁愿做
private void Update(){ Debug.Log(re.isVisible); if(!re.isVisible){ Destroy(re.gameObject);} },会发生什么?所以记录每个状态并销毁附加到渲染器的对象而不是这个游戏对象 -
尝试使用 FixedUpdate()
-
@NathanielCutajar 这应该有什么不同?
-
@derHugo 我在想这可能是对象之间的同步问题,如果已修复,它将一起完成。可能是错的
-
没问题,伙计们,我用 OnBecomeVisible() 和 OnBecomeInvisible() 替换了代码:-D 请支持这篇文章,因为我还没有声誉。