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?

