- Home /
Remove object based on its position alone
Language = Javascript
I have a number of terrain pieces tiled together.
At some point I want the Destroy them with some script.
But the terrain objects themselves are all clones.
All I need is something that allows me to grab / identify the object at an X and Z position that I already know.
ie:
Destroy ( get.gameObject(x,y,z) ) ;
I realise this code above doesn't work... but it gets my point across. Can anyone help me with this? I've heard about the Sphere overlap thing but it might be more complicated then I can handle. Is there any simple command?
Perhaps I could name the terrain object with their locations in their name instead and destroy them that way?
You can use brute force and get the list of every game objects : GameObject.FindObjectsOfType(typeof(GameObject)). Then, you go through them all, check the position, destroy if need be.
Or you can narrow that list down with Physics.OverlapSphere, if they have a collider.
If you know the objects that is to be destroyed from start then the best solution is to cache them when you create them. Example:
//Create
var tileObjects : GameObject[] = new GameObject[amountOfTiles];
for (var i=0; i<amountOfTiles; i++)
Instantiate(tilePrefab, Vector3(0,i,0), Quaternion.identity);
//Destroy
for (var tile : GameObject in tileObjects)
Destroy(tile);
If you want to do it for only certain objects within a position then a simple if-statement would do before you destroy the object.
They have a Terrain Collider. It is to be designed as an infinite tile-based terrain, so BruteForce is not wise. I only want the 1 64x64 terrain piece at say position (0,0,0) to be destroyed. Because the tile piece is random, I do not know what piece is placed.
$$anonymous$$oved to comments. It's not in the right order anymore though, sorry about that.
You could call Component.Broadcast$$anonymous$$essage() with a position as a parameter. Each Terrain piece could decide if it is close enough to the position to be destroyed and then destroy itself.
Answer by Jeffom · Jan 30, 2013 at 01:40 PM
try to make a dictionary to map the position of your objects (or create an xml) If you create your tiles on the fly do something like this
Dictionary< Vector3, Gameobject > myMap;
void CreateTile()
{
Gameobject myTileObj;
//blah blah create and place tile and set it to the myTileObj
//Supposing that 2 tiles will never have the same position then
if( !myMap.Contains( myTileObj.transfor.position ) )
myMap.add(Vector3, myTileObj);
}
Vector3 GetTileByPosition( Vector3 position )
{
if( myMap.Contains(position) )
return myMap[position];
}
tell us later if this works or not
I'll try this later today. Although I will mention that the tile data is already stored in a two-dimensional Array. The Array is in another script (and I'm not sure how I access it).
Your answer
Follow this Question
Related Questions
How do I set and get the position of a object? 3 Answers
C#: Changing current position of an object in y. 1 Answer
Object won't instantiate at parents position(solved) 2 Answers
Destroy and instantiate far objects 2 Answers
Random object placement. 1 Answer