- Home /
Erasing a gameobject if you have the coordinates.
Hi, I have a cube that moves left and right (2D). It moves on a grid of cubes (so a cube at x = 1 and y = 1, another cube at x=2 and y = 1 and so on). All the cubes are 1 by 1 by 1. My question is, if I press the down button, the cube closest under gets destroyed. Is there a way to destroy a game object if you have the coordinates? (btw, I'm working js). Thank you. -Nelis
Answer by Kiloblargh · Mar 07, 2013 at 06:34 PM
You don't need to use colliders for your simple application. Just a 2d array.
Add a reference to each object into the array when it is created.
var theGrid : GameObject[,];
var myTransform : Transform;
var myX : int;
var myY : int;
//in awake
theGrid = new GameObject[32,32];
//in the movement function
myX = Mathf.Round(myTransform.position.x);
myY = Mathf.FloorToInt(myTransform.position.y);
//in the dig function
if (theGrid[myX,myY-1])
{
Destroy(theGrid[myX,myY-1],0.1);
}
BTW are you making a Lode Runner derivative? That was one of my favorite ancient PC games.
I don't know that game but I don't think so. I think i get your way of doing it, but i don't know hope to incorporate it into my game. $$anonymous$$y project folder is in the latest comment to highpockets. Would you want to help me? (Btw, I'm making a game like $$anonymous$$otherload.)
I don't have Unity 4 yet and I'm too busy to really help; but I looked at your scripts. You should probably start over with a single script that controls all the gameplay. $$anonymous$$eep CameraScript separate, but all the others should be one script. The way you've set it up now, you will need to create script variables (the type of a js is its name) and drag the other scripts onto them on in the inspector, then Erase can access variables and functions in 2D$$anonymous$$apGrid, for example. But that structure gets tiresome fast and it will cause long build times if taken to an extreme.
Answer by Ankit Priyarup · Mar 07, 2013 at 05:25 PM
Search the game object which have following coordinates n java script then destroy it
I keep finding these by you, Please refrain from leaving this kind of one-line answer that just doesn't make sense. Use the add new comment button.
Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
Here at Unity Answers, Answer means Solution, not Response.
Watch this : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
Answer by highpockets · Mar 07, 2013 at 05:49 PM
Have you tried checking the isTrigger in your box collider? You can handle this through trigger events. For example, you could set a cube that is moving to layer 8 by using a bool to find out if it's moving. Then:
if( moving )
{
gameObject.layer = 8
}
Then you can check if that collider enters or stays on a trigger. OnTriggerEnter or OnTriggerStay. Here is an example with OnTriggerEnter.
function OnTriggerEnter( other : Collider )
{
if( other.gameObject.layer == 8 )
{
Destroy( gameObject );
}
Hope that helps.
One entity in the trigger must have a rigidbody. If you already handle movement without a rigid body, then check is$$anonymous$$inematic in the rigid body component.
I think I did what you said, but it's not working. Here is my project folder, I hope it clarifies what my intention is: http://www.mediafire.com/?4kndffgr84t9y8j BTW, I have deleted your code again in my script...
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Trying to make a simple inventory: 0 Answers
Confused about GameObject and Transform 1 Answer
What is wrong with my script? 1 Answer
Co Routine 2 Answers