我已经为可破坏的对象编写了一个脚本,但是它在一定时间后不会破坏被破坏的对象?
I have wrote a script for a destructible object, however it doesn't break the destroyed object after a certain time?
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),但破碎的瓶子仍然存在。
你销毁了错误的对象。
Instantiate 将原始对象作为参数,然后 returns 一个副本。
您应该将返回的对象分配给局部变量并将其传递给销毁调用:
var brokenBottleInstance = Instantiate(BrokenBottle,transform.position, transform.rotation);
Destroy(brokenBottleInstance, 1.5f);
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),但破碎的瓶子仍然存在。
你销毁了错误的对象。
Instantiate 将原始对象作为参数,然后 returns 一个副本。 您应该将返回的对象分配给局部变量并将其传递给销毁调用:
var brokenBottleInstance = Instantiate(BrokenBottle,transform.position, transform.rotation);
Destroy(brokenBottleInstance, 1.5f);