【问题标题】:destroying gameObject in both cases in Unity在 Unity 中的两种情况下都销毁游戏对象
【发布时间】: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 请支持这篇文章,因为我还没有声誉。

标签: c# unity3d


【解决方案1】:

在 FixedUpdate 函数中销毁 gameObject 部分

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    相关资源
    最近更新 更多