- Home /
Can't destroy Game Object in Array
Hello everyone! Sorry for my bad English :)
I have a map made of tiles. Using my script I generate some transparent tiles, which are located on the same coordinates as usual tiles. I need to destroy transparent tiles, which have no usual tiles under them. But they aren't destroyed. My code:
#pragma strict
var tiles : GameObject[,] = new GameObject [32,32];
var GridPrefab : GameObject;
var Contact = Array ();
var x : int;
var y : int;
function Start () {
for (var i = 0; i < y; i++)
for (var j = 0; j < x; j++)
tiles[j,i] = Instantiate (GridPrefab, Vector3 (j*10, 0, i*10), Quaternion.identity);
}
function Update () {
for (var i = 0; i < y; i++)
for (var j = 0; j < x; j++)
Contact = Physics.OverlapSphere (Vector3 (j*10, 0, i*10), 0.1);
if (Contact.length != 1)
Destroy (tiles[j,i]);
}
Try using Debug.Log(tiles[j,i].name);
ins$$anonymous$$d of Destroy(tiles[j,i]);
and see what the output is in the console.
It writes:
NullReferenceException: Object reference not set to an instance of an object Tile Array.Update () (at Assets/Scripts/Tile Array.js:17)
So... How object reference can be set to its instance?
Have you assigned reference to GridPrefab in the Editor?
Yes, of course. I have a map made of tiles, when I press the play button new tiles made of GridPrefab appear. In some places I have two tiles: one which was placed by me and another generated by script. I want to destroy tiles generated by script in places where they don't have premade twin
Are you getting any error or it just doesn't destroy the prefabs?
Answer by leooon12 · Mar 23, 2012 at 12:01 PM
That works good! A cup of coffee makes my brain think. I hope it can help someone in future.
#pragma strict
var tiles : GameObject[,] = new GameObject[32,32]; //Array for transparent grid
var GridPrefab : GameObject; //Prefab of transparent gri
var x : int; //Number of needed tiles
var y : int;
//Making a grid made of transparent prefabs
function Start () {
for(var i = 0; i < y; i++)
for(var j = 0; j < x; j++)
if(Physics.Raycast (Vector3(j*10, 1, i*10), -Vector3.up, 10)) //Cast a ray, which checks existence of a map tile. If there is no such tile transparent tile won't be generated
tiles[j,i] = Instantiate(GridPrefab, Vector3(j*10, 0, i*10), Quaternion.identity);
}
Answer by farooqaaa · Mar 23, 2012 at 04:59 AM
Replace Destroy (tiles[j,i]);
with:
if(tiles[j,i])
Destroy (tiles[j,i]);
Also replace Contact.length != 1
with Contact.length < 1
. Your code should look like this:
if (Contact.length < 1)
if(tiles[j,i])
Destroy (tiles[j,i]);
Now it doesn't get a mistake, but prefabs aren't destroyed. $$anonymous$$aybe I have a mistake in this part?
Contact = Physics.OverlapSphere (Vector3 (j*10, 0, i*10), 0.1);
if (Contact.length != 1)
If you use Physics.OverlapSphere (Vector3 (j*10, 0, i*10), 0.1); and there are no other objects around, it will return 1. So I tried if(Contact.length < 2); It didn't work. I have been trying to solve this problem for three hours already =) It seems to me I'm very very bad in scripting
It doesn't return 1 when there are no objects around. It returns the number of objects that are around. So in case there there's nothing around it will return 0.
It is cast from vector, not from object. In fact there is not important 1 or 2 - prefabs must be destroyed with any number in if(Contact.length < 2); because 0 and 1 are less then 2. So it isn't working. Thanks for your help, I'll try to figure out where is the problem hidden
Your answer
Follow this Question
Related Questions
Instantiating from an object that's in an array 1 Answer
Array problem -3 Answers
Find and store gameObjects in a array 2 Answers
Access a String array in one script from another script 0 Answers