【问题标题】:I have wrote a script for a destructible object, however it doesn't break the destroyed object after a certain time?我已经为可破坏对象编写了一个脚本,但是在一定时间后它不会破坏被破坏的对象吗?
【发布时间】:2020-12-04 19:06:12
【问题描述】:
public GameObject BrokenBottle;
AudioSource audioSource;

void Start()
{
    audioSource = GetComponent<AudioSource>();
}

private void OnCollisionEnter(Collision collision)
{
 if(collision.relativeVelocity.magnitude > 2)
    {
        
        Instantiate(BrokenBottle,transform.position, transform.rotation);
        Destroy(this.gameObject);
        Destroy(BrokenBottle,1.5f);
        audioSource.Play();
    
    }

}

}

我创建了一个脚本,当我打碎一个瓶子时,它会破坏未打碎的瓶子,然后实例化被破坏的瓶子。然而,我试图在 1.5f 之后摧毁破碎的瓶子(使用 GameObject BrokenBottle),但破碎的瓶子仍然存在。

【问题讨论】:

    标签: c# unity3d game-physics


    【解决方案1】:

    你破坏了错误的对象。

    Instantiate 将原始对象作为参数,然后返回一个副本。 您应该将返回的对象分配给局部变量并将其传递给destroy调用:

    var brokenBottleInstance = Instantiate(BrokenBottle,transform.position, transform.rotation);
    Destroy(brokenBottleInstance, 1.5f);
    

    【讨论】:

      猜你喜欢
      • 2010-12-25
      • 1970-01-01
      • 2018-02-26
      • 2014-05-26
      • 1970-01-01
      • 2020-11-17
      • 2019-03-01
      • 2021-06-22
      • 1970-01-01
      相关资源
      最近更新 更多