[英]Unity trying to access destroyed object
I instantiate a prefab object (bullet) and if it gets destroyed while the yield return seconds, Unity throws an error at the line where I set the rigidbody velocity to zero.我实例化了一个预制件 object(子弹),如果它在 yield return 秒时被销毁,Unity 会在我将刚体速度设置为零的那一行抛出错误。 It doesn't care about object null checks wherever I put them.
它不关心 object null 支票无论我把它们放在哪里。 The script is attached to the Instantiated gameobject.
该脚本附加到实例化的游戏对象。
public IEnumerator GetOutState (GameObject target) {
state = State.GetOut;
if (state == State.GetOut && gameObject != null) {
rbMissile.velocity = -transform.up * speed;
yield return new WaitForSecondsRealtime(1);
rbMissile.velocity = Vector2.zero;
StartCoroutine(FlyState(target));
}
}
What game object is this script attached to?这个脚本附在什么游戏object上? You are checking if the gameobject attached to the script is null with
您正在检查附加到脚本的游戏对象是否为 null
if (state == State.GetOut && gameObject != null) {
did you mean to put你是不是想放
if (state == State.GetOut && rbMissile != null) {
Also if this does not solve the issue, your issue is likely that:此外,如果这不能解决问题,您的问题可能是:
StartCoroutine(FlyState(target));
Something in the FlyState method is referencing the null game object FlyState 方法中的某些内容引用了 null 游戏 object
if如果
if (state == State.GetOut && rbMissile != null) {
didn't fix it your missle is getting destroyed between when this method is called and the one second delay from:没有修复它你的导弹在调用此方法和一秒延迟之间被摧毁:
yield return new WaitForSecondsRealtime(1);
Two possible fixes: change the IEnumerator to a void method and remove两个可能的修复:将 IEnumerator 更改为无效方法并删除
yield return new WaitForSecondsRealtime(1);
or else add in another check after a second passes或者在一秒钟后添加另一张支票
if (rbMissle != null) {
rbMissile.velocity = Vector2.zero;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.