c# – 销毁克隆会破坏所有克隆

前端之家收集整理的这篇文章主要介绍了c# – 销毁克隆会破坏所有克隆前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在一个特定的圆形区域内销毁一个对象的实例.代码如下:
Collider2D[] overlap = Physics2D.OverlapCircleAll(
    ball.transform.position,(ball.renderer.bounds.size.x)/2);
if (overlap.Length>=1)
{           
    foreach (Collider2D coll in overlap)
    {
        Debug.Log (coll.GetInstanceID());
        if (coll.name.Contains("alien"))
        {
            //problem here:
            Destroy (coll.gameObject);
        }
    }
}

Destroy(coll.gameObject)永久地破坏了所有克隆,并且没有实例化新的克隆并且我得到错误MissingReferenceException:类型’GameObject’的对象已经被破坏但你仍然试图访问它.
您的脚本应该检查它是否为null或者您不应该销毁该对象.

有没有办法摧毁那个特定的克隆?我尝试了不同的名称并使用Destroy(GameObject.Find(coll.name)),但它也会破坏所有克隆并阻止新的产生.

有人帮忙吗?

更新:

实例化如下:

private bool bCanCreateParachuter = true; // bool to stop the spawning
GameObject go;


// Use this for initialization
void Start () {

    //handling screen orientation
    Screen.orientation = ScreenOrientation.LandscapeLeft;
    ///

    go = (GameObject)Instantiate(Resources.Load("alienPink")); 
    StartCoroutine("CreateParachuter");

}



IEnumerator CreateParachuter()
{
    while(bCanCreateParachuter)
    {

        Instantiate(go,new Vector3(Random.Range(-10,10),Random.Range(-5,5),0),Quaternion.identity);
        //          Instantiate(go,Random.Range(-10,Quaternion.identity);
        go.name = "alienPink"+nextNameNumber;
        nextNameNumber++;
        yield return new WaitForSeconds(Random.Range(0f,1f));
        yield return null;
    }
    yield return null;
}

关键更新:

如果我取消注释if(grabbedObject!= null)in,代码可以工作

//  if (grabbedObject != null) {

//works if uncomment above for some reason

        Collider2D[] overlap = Physics2D.OverlapCircleAll (ball.transform.position,(ball.renderer.bounds.size.x)/2);
        if (overlap.Length>=1){

            foreach (Collider2D coll in overlap){
        Debug.Log (coll.GetInstanceID());
            if (coll.name.Contains("alien")){
                    Destroy (coll.gameObject);

            }
            }
        }else {
        //  Debug.Log (grabbedObject.renderer.bounds.size.x);
        }

这是grabbedObject的背景:

Rigidbody2D grabbedObject = null;
. . .
RaycastHit2D hit = Physics2D.Raycast(mousePos2D,dir);

        //if (hit!=null && hit.collider!=null){

        // check collisions with aliens





    //  OnCollisionEnter2D(grabbedObject.collisionDetectionMode);


        if ( hit.collider!=null){
            // we clicked on something lol... something that has a collider (Box2d collider in this case)
            if (hit.collider.rigidbody2D!=null){
                //hit.collider.rigidbody2D.gravityScale = 1;
                grabbedObject = hit.collider.rigidbody2D;
            //  circleCollider = hit.collider.collider2D.   ;


                springJoint = grabbedObject.gameObject.AddComponent<SpringJoint2D>();
                // set the anchor to the spot on the object that we clicked
                Vector3 localHitPoint =  grabbedObject.transform.InverseTransformPoint(hit.point);
                springJoint.anchor  = localHitPoint;
//      



dragLine.enabled = true;
                }

            }

基本上,grabbedObject是你在屏幕上点击并拖动的任何东西(任何GameObject),我在这里想念的是什么?

解决方法

重生问题是您没有保存对资源项的引用,因此当您销毁第一个项时,您创建实例化的“模板”将被销毁

这可以解决这个问题

GameObject template;
void Start()
{
     //handling screen orientation
     Screen.orientation = ScreenOrientation.LandscapeLeft;
     template = (GameObject)Resources.Load("alienPink");
     StartCoroutine("CreateParachuter");
}

IEnumerator CreateParachuter()
{
     while(bCanCreateParachuter)
     {
        GameObject go = Instantiate(template,1f));
        yield return null;
    }
    yield return null;
}

在破坏所有克隆方面,你的调试日志是说它正在销毁多个项目吗?如果是这样,碰撞可能确实击中了所有克隆,从而将它们全部摧毁.

猜你在找的C#相关文章