Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Paralarax · Nov 20, 2021 at 07:33 PM · instantiateprefabsbuildingclonegrid based game

Destroy button only destroys the last clone of object

Hello, I am working on a 2d isometric grid building system, and I have encountered a problem with destroying already placed objects. So I have a prefab with a panel and buttons, one button for placement and the other for destroying the prefab clones. It works but only for the prefab clone that was placed last. If I try to destroy others I get this error "MissingReferenceException: The object of type 'Building' has been destroyed but you are still trying to access it.".

Here is the code I use to instantiate objects

  public void InstantiateObject(GameObject gameObject)
     {
         if(!objectUnplaced)
         {
             if (canBePlaced)
             {
                current=Instantiate(gameObject,Vector3.zero,Quaternion.identity).GetComponent<Building();
                 GridBuildingSystem.instance.FollowBuilding(current);
                 towerUnplaced = true;
             }
         }
 
     }

This method is called from buttons script:

  public void DestroyObject()
     {
         Destroy(gameObject);
     }


I understand what the error means but I don't really know how to solve it, does anybody have any suggestions on how to fix it?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Dsiak · Nov 21, 2021 at 08:26 AM

You shouldn't use "gameObject" as a parameter name because "gameObject" is the Game Object the script is attached to, which also means your DestroyObject function is probably only deleting the Game Object the script is attached to. Maybe this is creating the problem

Try using a different name and see if it helps

Comment
Add comment · Show 6 · 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 Paralarax · Nov 21, 2021 at 02:57 PM 0
Share

Thank you, but it still doesn't work. I should have worded this question better and also describe my issue properly, here is a video I recorded that shows what I need to do: video

Do you know how can I fix this? Sorry for bothering you.

avatar image Dsiak Paralarax · Nov 21, 2021 at 04:33 PM 1
Share

Sorry if I'm not any help I ant a master programmer.

It looks like you have a very solid system already, can you try to following see if it works? Maybe a variable of type Building called "currentlySelectedBuilding" is set every time you click one of the buildings and then "DestroyObject" destroys the "currentlySelectedBuilding.gameObject"

avatar image Paralarax Dsiak · Nov 21, 2021 at 07:08 PM 0
Share

It doesn't work but now looking at my code my Building current is public static? Can this be the reason why it doesn't work?

Edit: So, I was playing around and turned off the buttons and added if statement for destroying with ESC in my dragging function, and instead of Destroy(gameObject or current.gameObject) I used transform.gameObject, and it kinda works but only if I don't place the object and I still get the error once I delete an object that was instantiated last but it continues to work, so it seems that there is a problem with public static Building. What do you think?

Show more comments

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

201 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get the position of a new Instantiated prefab 0 Answers

Issue with instantiated text clone 0 Answers

Destroy the first instantiated clone after 4 have been instantiated 1 Answer

Changing rotation of prefab 0 Answers

Saving Instantiated objects,Saving Instantiated objects 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