破坏克隆会破坏所有克隆

问题描述 投票:7回答:2

我想在一个特定的圆形区域内销毁一个对象的实例。代码如下:

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: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

是否有办法销毁该克隆,并且具体克隆该克隆?我尝试使用不同的名称并使用Destroy(GameObject.Find(coll.name)),但这也会破坏所有克隆并防止产生新的克隆。

有人帮忙?

UPDATE:

说明如下:

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, new Vector3(Random.Range(-10,10), Random.Range(-10,10), 0), Quaternion.identity);
        go.name = "alienPink"+nextNameNumber;
        nextNameNumber++;
        yield return new WaitForSeconds(Random.Range(0f,1f));
        yield return null;
    }
    yield return null;
}

重要更新:

如果取消注释if (grabbedObject !=null),则代码有效

//  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),在这里,我想念的是什么?

c# unity3d game-engine unityscript
2个回答
0
投票

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

这将解决此问题

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, new Vector3(Random.Range(-10,10), Random.Range(-5,5), 0), Quaternion.identity);
        go.name = "alienPink"+nextNameNumber;
        nextNameNumber++;
        yield return new WaitForSeconds(Random.Range(0f,1f));
        yield return null;
    }
    yield return null;
}

就销毁所有克隆而言,您的调试日志是否指出销毁多个项目?如果是这样,碰撞确实可能击中了所有克隆,因此将它们全部摧毁。


0
投票

[这是一种理论,我认为销毁对象时也会从资源中销毁它,因此其他实例无可参考,因此,如果您仅从预制的[SerializeField]预制对象中引用该对象,该对象似乎总是对我有用。如果您想澄清,请回复。

© www.soinside.com 2019 - 2024. All rights reserved.