- Home /
Destroy(this.Object) destroys ALL instances
Hello guys. I am trying to make a two dimmensional field for growing crops, that will be saved in a 2D array [so it doesn't get destroyed when the player changes a map and comes back, and can be saved to a file]
I have made it instantiate a "Field" prefab in a nested FOR loop [later on it will read values from the mapData, and place the plants accordingly] - the problem is, when I try to detect a mouse click, as a test, for one of the fields, and make it get destroyed, ALL of the instances [fields] get destroyed by that. I have dug throught google for similar problems, but none seemed to help me in fixing that problem.
The method which instantiates the map [no data reading yet, just a simple instantiate]:
public void generateMap()
{
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
GameObject newField = (GameObject)Instantiate(emptyField, new Vector3(11.5f + x, 1.5f + y, 0), Quaternion.identity);
//FieldData fData = newField.GetComponent<FieldData>();
//fData.fieldPos = new Vector2(x, y);
}
}
}
The code for destroying a field on mouse click [is inside the script attached to each field]:
void Update () {
if (Input.GetMouseButton(0))
{
setFieldID(0);
}
}
Gah, it broke the first code sample somehow >_>
Here it is again:
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
GameObject newField = (GameObject)Instantiate(emptyField, new Vector3(11.5f + x, 1.5f + y, 0), Quaternion.identity);
//FieldData fData = newField.GetComponent<FieldData>();
//fData.fieldPos = new Vector2(x, y);
}
}
You are not saving anything here to a 2D array. How do you assign the values to your mapData array?
I do not do that yet, in fact all I do is creating 10 x 10 field of prefabs, each having a script attached to it that should destry it on click - ins$$anonymous$$d, when I click one it destroys ALL of them.
The 2D array is going to be used for saving the contects [empty/plantType] so that I can reinstantiate the whole field when the player changes the map, and comes back AND save it to a file later on.
Here's how the field looks like in game [just for the sake of imagining it properly] Each field is an instantiated prefab
Answer by robertbu · Jan 12, 2014 at 06:17 PM
I assume 'setFieldID(0)' method destroys the object? Right now you are using Input.GetMouseButton(0) which will return true for all of your objects. One approach is to use OnMouseButtonDown() instead:
void OnMouseButtonDown()
{
setFieldID(0);
}
Another approach is to use Raycasting. That approach is a bit more complicated, but is more mobile friendly.
Yes, it destroys the object, as a test [doesn't do anything with the ID yet]
You are right, I feel so dumb now. I was pretty much sure it will check if the mouse was clicked on that particular object [It's a 2D sprite by the way, so collider2D], and not that it will just check if the mouse is down ..
I will try just that
The collider2D does not have bounds.Contains, any idea of what do I use ins$$anonymous$$d?
Answer by Jamora · Jan 12, 2014 at 06:14 PM
You're not checking which field is being clicked. Input.GetMouseButton will be true on all scripts when the mouse is clicked, thus it sets each fields id to 0.
You will need to check which field contains the mouse cursor and set that field's id to 0. Assuming you have a collider on the GOs, then something like
if (Input.GetMouseButton(0) && collider.bounds.Contains(Input.mousePosition))
should work.
I would recommend, however, that you use the OnMouseDown function.
so instead of having that in your Update, just have
void OnMouseDown(){
setFieldID(0);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613130212im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Destroy all previous clones. 2 Answers
Destroy all objects in the scene? 0 Answers
Destroying an instance problem 1 Answer
Destroy All of One One object on collision 1 Answer
Destroy instances created continuosly of a prefab in a network game... 0 Answers