Problem with instatiating gameobjects [Unity 2D]

Hi guys,

i got a little problem and hope you can help me =)

Im instantiating gameobjects randomly with the help of a for-loop (see: " //create inner blocks and walls"):

using UnityEngine;
using System.Collections;

public class init : MonoBehaviour {

    public GameObject wallprefab;
    public GameObject blockprefab;


    // Use this for initialization
    void Start ()
    {
        //create outer walls
        for (int p = -7; p < 8;p++)
        {
            GameObject dummy = Instantiate(wallprefab) as GameObject;
            dummy.transform.position= new Vector2(p,7);
        }
        for (int p = -7; p < 8;p++)
        {
            GameObject dummy = Instantiate(wallprefab) as GameObject;
            dummy.transform.position= new Vector2(p,-7);
        }
        for (int p = -6; p < 7;p++)
        {
            GameObject dummy = Instantiate(wallprefab) as GameObject;
            dummy.transform.position= new Vector2(7,p);
        }
        for (int p = -6; p < 7;p++)
        {
            GameObject dummy = Instantiate(wallprefab) as GameObject;
            dummy.transform.position= new Vector2(-7,p);
        }

        //create inner blocks and walls
        for (int x = -6;x < 7;x++)
        {
            for (int y = -6;y < 7;y++)
            {
                int indicator = Random.Range (1,7);


                switch(indicator)
                {
                    case 1:
                    case 2:
                    case 3:
                       
                    break;
                    case 4:
                    case 5:

                        Instantiate(wallprefab,new Vector2(x,y),transform.rotation);
                    break;
                    case 6:
                        Instantiate (blockprefab,new Vector2(x,y),transform.rotation);
                    break;
                }


            }
        }

    }
   
    // Update is called once per frame
    void Update () {
   
    }
}

After the objects are instantiated it looks like this:

You can see, that the instantiated objects got the tag “block”. Now im trying to destroy some of these when they are in the bounds of another dynamically instantiated gameobject with the help of this code:

void damage()
    {
        if (gameObject.GetComponent<Collider2D>().bounds.Contains (GameObject.FindGameObjectWithTag ("block").transform.position))
        {
            GameObject block = GameObject.FindGameObjectWithTag ("block");
            Destroy(block);
        }
    }

The problem is that the script doesnt find objects with this tag. But if i instantiate one single object with the tag “block” it can be found…
What am i doing wrong?

Can’t see that at all actually, since it’s cut off in the picture. Maybe you’re referring to the sprite being called “block”? That’s a completely different thing, if that’s what you mean. First thing’s first, you should assign “block” to the tags of all newly instantiated objects, explicitly just after creation. That’ll tell us if the tag is the problem.

Is there a specific reason you aren’t using collision events for this? You have the collider there already, so maybe you should just use OnCollisionEnter instead (with will have a reference to the colliding object right there in the Collision parameter). Iterating over all of the GameObjects in the scene looking for the one that may or may not be within a bounds is going to be terribly slow, and you’re doing it twice in a row. On top of that, you’re only doing FindGameObjectWithTag, which is going to find the first object in the scene with that tag and then stop, and return it. It won’t iterate over all of them, so if that first block doesn’t happen to be the one inside of your “bounds”, nothing’s going to happen- it won’t check the rest.

The whole process seems really inefficient. When you’re instantiating new objects, at least put them in a list so that you can iterate over that list instead of “all objects in the scene” this way. If all of your “blocks” are children of one object, then referencing that object and iterating over its children, or Components within the children, would be faster than this too.


Woops, here we got the right image. Now you can see that the tag “block” is assigned to the gameobject. I would say the tag isnt the problem, because if i instantiate the same object like so:

Instantiate (blockprefab,new Vector2(x,y),transform.rotation);

without any loop around it it works…

I dont use collision events because i thought the objects have to move into each other to trigger the collision event. Since i instantiate the object with the second script right on the “block”-tagged one, there is no motion.