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
0
Question by DMcB · Jun 12, 2013 at 05:58 AM · gameobjectinstantiateprefabdestroyclone

Deleting a GameObject

This question seem to get asked a lot, which makes it slightly more embarrassing that I can't get it to work!

I am trying to delete an Instantiated prefab that a user selects, I've checked through loads of questions on this very subject, but using Destroy doesn't seem to work :/

 //class level variable to store the object the user has selected
 private GameObject selectedItem;
 
 ...
 
 public void OnGUI (){        
     //if delete button is pressed
     if (GUI.Button(...))
     {            
         Destroy (selectedItem);    
     }            
 }
 
 ...
 
 //use Raycast to detect the GameObject the user has touched 
 //this is called from within Update() stack
 if (Physics.Raycast(cursorRay, out hit, Mathf.Infinity, this.layerMask)){
     switch (hit.transform.tag) {
         case "tag":                                                
             selectedItem = hit.transform.gameObject;
                         break;

As far as I can tell, it sets the GameObject fine (it changes its color in other parts of the code), but the Destroy will not get rid of it!

Has anyone got any ideas? I know this isn't much code to go by, but if you need more info/code let me know!

Cheers,

Dave McB

Comment
Add comment · Show 2
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 sdgd · Jun 12, 2013 at 06:10 AM 0
Share

that's because your GameObject is private

how are you referencing witch gameobject is this: selectedItem

as all you said how you reference it is:

 ...

not much info right, ...

well anyway I suggest you make it public and drag GO from inspector in to the script ;)

allso stripping code down as much as possible is gr8 to get good answers so no unwanted garbage is in the way of problem but everything that does change or does have effect on your question does need to be inside the code.

like if you'd skip :

 private GameObject selectedItem;

and you say in your code

 prvate GameObject selectedItem;

so make an typo and not posting it is like chasing shadows in the dark for us

avatar image DMcB · Jun 12, 2013 at 01:06 PM 0
Share

Yeah, the whole make it easier to understand is a bit of a catch 22 :(

Dragging the GO onto the script doesn't seem to make the item appear in the Heirarchy, but the Destroy function now throws an error saying:

 Destroying assets is not permitted to avoid data loss.
 If you really want to remove an asset use DestroyImmediate    (theObject, true);

However DestroyImmeadiate seems to get rid of every single instance of that prefab (not what I want)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MaT227 · Jun 12, 2013 at 06:16 AM

Like sdgd, I would say, first make your GO public so you can check in the inspector if it's well set and if it corresponds to the right GO in the scene. Next you can try other function like DestroyImmediate. You can also check if there is no error in the debug.

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

Answer by lighting · Jun 12, 2013 at 06:39 AM

The cause that came up to my mind is that this GO is actually being destroyed but another Raycast (because it's being called in Update()) assigns this GO again and again (line 21 in your code). Please, check also if in other places in your code, you don't reinitialize this GO.

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

17 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

Related Questions

Instantiating prefabs: "The object of type GameObject has been destroyed". 1 Answer

Accessing children of an instance doesn't work every time 1 Answer

Trouble with destroying an instantiated prefab 2 Answers

Assign an instantiated GameObject? 1 Answer

Check if object is destroyed on level load, if so instantiate prefab? 1 Answer


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