- Home /
Tile generation and on click.
Hello,
I am making a 2D platform game with an level editor. I have made a tile generater. And a little debug script to change the tile. The problem is when i click on a tile every tile will be destroyd my destroy script:
[code]void Update () {
if (Input.GetMouseButtonDown(0)){
Destroy(gameObject);
}
}[/code]
This script is applyd to every tile.
void Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0)){ // if left button pressed...
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)){
// the object identified by hit.transform was clicked
Destroy(this.gameObject);
// do whatever you want
}
}
}
Also this doesn't work
Answer by NecrosDk · Mar 16, 2014 at 03:21 PM
You need to refer to hit.transform.gameObject instead of this.gameObject.
Basically what you are doing right now is checking if the ray hits anything, in ALL the tiles, if the ray hits anything it calls destroy in every tile.
I advise that you set up one gameobject which is in charge of the Raycasting. This way you dont run x amounts of raycasting every frame where x is every tile.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Vehicle Script Help 1 Answer
Spawn Script Not Working 1 Answer
GUItexture change on mouseenter 3 Answers