Destroying clone destroys all clones

I want to destroy an instance of an object when it is in a specific circular area. The code is as follows:

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) permanently destroys all clones, but no new ones are created, and I get an error message 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. 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.

Is there a way to destroy this and this clone in a particular? I tried different names and used Destroy(GameObject.Find(coll.name)) , but also destroyed all the clones and prevented new ones from appearing.

Anyone help?

UPDATE:

It is carried out as follows:

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

Key update:

The code works if I uncomment 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); } 

This is the background of 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; } } 

Basically, grabbedObject is all that you click and drag on the screen (any GameObject), what am I missing here guys?

+7
c # unity3d unityscript game-engine
source share
1 answer

The problem that arises is that you are not saving the reference to the resource element, so when you destroy the very first element that you create to create an instance, to destroy

That would allow it.

 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 terms of destroying all clones, is this your debug log stating that it destroys several elements? If so, then a clash can really hit all the clones and, therefore, destroy them all.

0
source share

All Articles