2015-05-20 12 views
7

Voglio distruggere un'istanza di un oggetto quando si trova all'interno di una particolare area circolare. Il codice è il seguente:Distruggere il clone distrugge tutti i cloni

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); 
     } 
    } 
} 

Il Destroy(coll.gameObject) distrugge tutti i cloni permanantly e quelli nuovi non sono istanziati e ottengo l'errore 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.

C'è un modo per distruggere quello e quello clone specifico? Ho provato nomi diversi e usando Destroy(GameObject.Find(coll.name)) ma questo distrugge anche tutti i cloni e impedisce a quelli nuovi di spawnare.

Qualcuno aiuto?

UPDATE:

Instantiating come segue:

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; 
} 

Aggiornamento cruciale:

Il codice funziona se Decommentare 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); 
     } 

Questo è lo sfondo della 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; 
       } 

      } 

Fondamentalmente la grabbedObject è tutto quello che il mouse e trascinando sullo schermo (qualsiasi GameObject), quello che mi sto perdendo qui ragazzi?

+0

Come stai creando cloni di 'gameObject'? – Dai

+0

Probabilmente qualcosa non va con il tuo codice "clone" ... o 'Destroy' - assicurati di pubblicare il più piccolo insieme di codice che mostri un problema con tutti i pezzi correlati. –

+0

Post originale aggiornato per includere l'istanza –

risposta

0

Il problema respawning è che non sta salvando un riferimento alla voce risorse, in modo che quando si distrugge il primo elemento che si crea il "modello" per istanziare viene distrutto

Questo risolverebbe questo

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; 
} 

In termini di distruzione di tutti i cloni è il registro di debug che indica che sta distruggendo più elementi? Se è così, la collisione potrebbe effettivamente colpire tutti i cloni e quindi distruggerli tutti.

+0

Questo non ha funzionato Colton. Nessun collisore non li colpisce tutti. Ho intenzione di aggiornare alcuni dettagli. –

+0

Spiacente dovrebbe essere "Instatiate (template, ..." non "Instatiate (go ...." che dovrebbe risolvere il problema di instatiating che non funziona. –

+0

Cosa ottieni se cambi il debug in Debug.Log (coll .gameObject.name)? Che nome si ottiene? E se qualcuno dei tuoi oggetti viene eliminato come genitore degli altri oggetti? la distruzione del gameObject cancella anche tutti i suoi figli –

Problemi correlati