Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Gebreel · Jan 12, 2014 at 06:03 PM · destroyinstancesall

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);
         }
 
 }

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gebreel · Jan 12, 2014 at 06:03 PM 0
Share

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);
             }
         }
avatar image robertbu · Jan 12, 2014 at 06:07 PM 0
Share

You are not saving anything here to a 2D array. How do you assign the values to your mapData array?

avatar image Gebreel · Jan 12, 2014 at 06:12 PM 0
Share

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.

avatar image Gebreel · Jan 12, 2014 at 06:14 PM 0
Share

Here's how the field looks like in game [just for the sake of imagining it properly] Each field is an instantiated prefab

alt text

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gebreel · Jan 12, 2014 at 06:20 PM 0
Share

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

avatar image Gebreel · Jan 12, 2014 at 06:24 PM 0
Share

The collider2D does not have bounds.Contains, any idea of what do I use ins$$anonymous$$d?

avatar image Gebreel · Jan 12, 2014 at 06:30 PM 0
Share

Got it, OverlapPoint. It works now, thanks for help!

avatar image
1

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);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges