C# 在这两种情况下,在Unity中销毁游戏对象
我写了一个代码,当玩家没有在相机中渲染时,它应该被销毁,但即使在相机中渲染,它也会被销毁,请参阅我下面的代码C# 在这两种情况下,在Unity中销毁游戏对象,c#,unity3d,C#,Unity3d,我写了一个代码,当玩家没有在相机中渲染时,它应该被销毁,但即使在相机中渲染,它也会被销毁,请参阅我下面的代码 using UnityEngine; public class IfnotvisibleDestroy : MonoBehaviour { public SpriteRenderer re; // Start is called before the first frame update void Start() {
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);
}
}
}
FixeUpdate函数中是否包含销毁游戏对象部分您确定日志和销毁发生在同一帧中,该组件和渲染器的同一实例中吗?如果您宁愿执行
private void Update(){Debug.Log(re.isVisible);如果(!re.isVisible){Destroy(re.gameObject);}}
,会发生什么?因此,记录每个状态并销毁连接到渲染器的对象,而不是此gameObjectTry使用FixeUpdate()@NathanielCutajar,这会有什么区别?@derHugo我想这可能是对象之间的同步问题,如果它被修复,它将结合使用。可能是错误的,没问题,伙计们,我用OnBecomeVisible()和OnBecomeVisible()替换了代码:-D请投票支持这篇文章,因为我还没有反驳。