Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 如果施力,Unity中的池中游戏对象在SetActive后会自我毁灭_Unity3d_Destroy_Object Pooling - Fatal编程技术网

Unity3d 如果施力,Unity中的池中游戏对象在SetActive后会自我毁灭

Unity3d 如果施力,Unity中的池中游戏对象在SetActive后会自我毁灭,unity3d,destroy,object-pooling,Unity3d,Destroy,Object Pooling,当我从创建的对象列表中检索一个对象并重新激活它时,它会破坏自身,但只有当我施加了力使其开始移动时,它才会破坏自身。如果我从不施力,一切都会按预期进行,我可以不断地激活物体 我试着在OnCollision中放一个debug.log,它没有与任何东西发生冲突 如上所述,如果我从未开始加速,其余的都可以 当施加速度时,第一个射弹起作用,第二个射弹自毁 我尝试过手动激活它们,而不是通过代码。同样的结果 例外情况:如果我自己通过inspector手动激活/停用对象,那么如果我在池中只有一次快照,它们就会

当我从创建的对象列表中检索一个对象并重新激活它时,它会破坏自身,但只有当我施加了力使其开始移动时,它才会破坏自身。如果我从不施力,一切都会按预期进行,我可以不断地激活物体

  • 我试着在OnCollision中放一个debug.log,它没有与任何东西发生冲突
  • 如上所述,如果我从未开始加速,其余的都可以
  • 当施加速度时,第一个射弹起作用,第二个射弹自毁
  • 我尝试过手动激活它们,而不是通过代码。同样的结果
  • 例外情况:如果我自己通过inspector手动激活/停用对象,那么如果我在池中只有一次快照,它们就会工作
  • 如果我通过代码执行一次性规则,它就不起作用。即使我只有一次机会也会失败
  • 当我不使用池时,所有的代码都工作了
繁殖代码:

    void CreateBullet(int GunID)
{
    GameObject ShotFired = Guns[GunID].GetComponent<Weapon>().FireWeapon();

    if (ShotFired == null)
    {
        ShotFired = Instantiate(Guns[GunID].GetComponent<Weapon>().Projectile,Guns[GunID].gameObject.transform);
        Physics.IgnoreCollision(ShotFired.GetComponent<SphereCollider>(), Guns[GunID].GetComponentInParent<CapsuleCollider>());
        Guns[GunID].GetComponent<Weapon>().AmmoPool.Add(ShotFired);
    }


    ShotFired.transform.position = Guns[GunID].transform.position; 
    ShotFired.transform.rotation = Guns[GunID].transform.rotation;

    ShotFired.SetActive(true);
}
void CreateBullet(int-GunID)
{
GameObject shotFireed=枪[GunID].GetComponent().FireEapon();
如果(ShotFired==null)
{
ShotFired=实例化(枪[GunID].GetComponent().Sproject,枪[GunID].gameObject.transform);
Physics.IgnoreCollision(ShotFired.GetComponent(),Guns[GunID].getcomponentipanart());
Guns[GunID].GetComponent().AmmoPool.Add(散弹射击);
}
ShotFired.transform.position=枪[GunID].transform.position;
ShotFired.transform.rotation=枪[GunID].transform.rotation;
ShotFired.SetActive(true);
}
射弹代码:

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class Projectile : NetworkBehaviour
{
    public float Speed;
    public float LifeTime;
    public float DamagePower;

    public GameObject ExplosionFX;

    // Start is called before the first frame update
    void Start()
    {
        GetComponent<Rigidbody>().AddRelativeForce(Vector3.up * Speed, ForceMode.VelocityChange);
        StartCoroutine(DeactivateSelf(5.0f));
    }

    private void OnDestroy()
    {
        Debug.Log("WHY!");
    }

    IEnumerator DeactivateSelf(float Sec)
    {
        yield return new WaitForSeconds(Sec);
        Explode();
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.GetComponentInChildren<Vehicle>())
        {
            collision.gameObject.GetComponentInChildren<Vehicle>().CmdTakeDamage(DamagePower);
        }

        Explode();
    }

    void Explode()
    {
        GameObject ExplosionEvent = Instantiate(ExplosionFX, this.transform.position, Quaternion.identity);
        NetworkServer.Spawn(ExplosionEvent);
        GetComponent<Rigidbody>().AddRelativeForce(Vector3.zero);

        gameObject.SetActive(false);
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用镜子;
公共类:网络行为
{
公众浮标速度;
公共浮动寿命;
公共权力;
公开游戏对象ExplosionFX;
//在第一帧更新之前调用Start
void Start()
{
GetComponent().AddRelativeForce(Vector3.up*速度,ForceMode.VelocityChange);
启动例行程序(停用自身(5.0f));
}
私有void OnDestroy()
{
Log(“为什么!”);
}
IEnumerator停用自我(浮动秒)
{
收益返回新的WaitForSeconds(秒);
爆炸();
}
专用void OnCollisionEnter(碰撞)
{
if(collision.gameObject.getComponentChildren())
{
collision.gameObject.getComponentChildren().CmdTakeDamage(DamagePower);
}
爆炸();
}
空洞爆炸()
{
GameObject ExplosionEvent=实例化(ExplosionFX,this.transform.position,Quaternion.identity);
NetworkServer.Spawn(爆炸事件);
GetComponent().AddRelativeForce(Vector3.zero);
gameObject.SetActive(false);
}
}

欢迎有任何想法。提前谢谢

如果其他人偶然发现了这一点,下面是发生的情况,以及为什么为对象指定移动速度是导致问题的原因

我的射弹上有个“叛徒”

默认情况下,“Autodestruct”为true

自动销毁不会销毁轨迹,但会在轨迹消失时销毁游戏对象。因此,如果您禁用任何具有轨迹的对象,并通过移动将其激活,当对象停止移动(例如在对象池中重新定位)时,它将销毁父对象

如果对象从不移动,它就不会产生轨迹,也不需要销毁

要修复,只需取消选中autodestruct


这是唯一的脚本吗?没有其他人会破坏任何东西吗?没错。我在Destroy.object上做了一个搜索,只是为了确定,代码中没有其他东西会破坏它。你能在github上发布一个最小的项目,这样我们就可以完整地看到它吗?我刚刚找到了它。其中,它是TrailRender组件。如果我把它从对象上取下来,脚本和所有的东西都会工作。穿上它。。。繁荣如果我把轨迹放在子对象上,我不会得到错误,但是新的投射物没有轨迹。我会把这件事弄得一团糟,如果我明天不能弄清楚,我会制作一个最小版本来直接显示问题。