• 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 TheEmeralDreamer · Sep 18, 2011 at 07:49 AM · destroy-clones

Deleting A variable's contents

How would I go about deleting a variable's contents if it's a game object WITHOUT destroying the variable? In this case it's an instance of a prefab. How would I delete it ? I wouldn't want said object to hang around forever.

Comment
Add comment · Show 7
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 ShinAli · Sep 18, 2011 at 07:56 AM 0
Share

I'm not sure if I still understand based on from your previous post. Are you concerned that the object would still remain in memory after you Destroyed it from your scene and nulled out all references?

avatar image TheEmeralDreamer · Sep 18, 2011 at 08:02 AM 0
Share

$$anonymous$$y goal is this. I have a system in place that allows me to slot 4 weapons. I have it set that i can equip said weapons on the fly. What I want is to delete any lingering clones , because they randomly position themselves around the scene. I think I just had a really good idea tho.. let me test it...

avatar image asafsitner · Sep 18, 2011 at 08:20 AM 0
Share

You can perhaps disable the objects you don't want to use at the moment. That way they won't render and any scripts attached won't work, but you won't have to destroy/instantiate them every time. Just play the switching animation and you're done. You can even set events on the animation to do the enable/disable thing.

avatar image TheEmeralDreamer · Sep 18, 2011 at 08:57 AM 0
Share

I can't disable them i have 30 weapons and it would get messy to say the least since I also have 5 weapon points that are parented to the right hand.

I think what I can do is create an array of instances of all the weapons and switch them out ins$$anonymous$$d of delete them, as you suggested.

avatar image TheEmeralDreamer · Sep 18, 2011 at 08:58 AM 0
Share

However my original idea was to find the object by child relation, delete it, then simply instantiate like I was going to.

Show more comments

1 Reply

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

Answer by syclamoth · Sep 18, 2011 at 08:45 AM

I don't understand what you are talking about here- it is impossible to destroy a variable! A variable is a named link to some other object- if you delete a variable's contents, the variable itself will still exist, except that its contents will become 'null'.

As for slotted weapons, do you want the player to be able to drop weapons and pick them up later? Or do you use a separate 'pickup' object for that? If you are talking about the weapons that a player actually uses, why not just not delete them at all? Just make them invisible until your player wants to use that particular weapon again, and if they're not carrying the right token ("game logic" weapon, as opposed to graphics etc. weapon), then just don't let them take it out.

Comment
Add comment · Show 5 · 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 TheEmeralDreamer · Sep 18, 2011 at 08:52 AM 0
Share

$$anonymous$$aybe that's why you don't understand. I never asked asked anything about destroying the variable lol.

I asked how to $$anonymous$$ETE an object stored inside a variable. Not set it to null, because that doesn't delete it.. it simply displaces it from the variable and places it somewhere else.

avatar image asafsitner · Sep 18, 2011 at 09:00 AM 0
Share

If you want to delete a gameObject you use Destroy(); that's how it goes. But you said you already knew that and want something else. If you just want to turn it invisible then disable it altogether (mesh renderer and any logic scripts). If you want something else then please be more precise with your description.

avatar image syclamoth · Sep 18, 2011 at 09:01 AM 0
Share

Oh well that's pretty straighforward. Use Object.Delete on the variable (which is to say, call Delete(variablename)) This deletes the variable's contents, and then resets the variable to null. What I don't understand, is why it moves around when it gets dereferenced- does the object in question have some default behaviour which get overriden when it's in the player's hand?

avatar image TheEmeralDreamer · Sep 18, 2011 at 09:03 AM 0
Share

@ asafsitner I can't be more precise than telling you i want to delete the contents of a variable when the contents are an object. There's a difference in syntax though when I want to $$anonymous$$ETE the object using a variable that is referencing it, as syclamoth just pointed out.

@ syclamoth Also, I've tried that. It doesn't work, Unity throws a "doesn't want to delete assets at runtime" error. Also tried DestroyImmediate and it deleted all the instances and the prefab.

avatar image asafsitner · Sep 18, 2011 at 09:08 AM 0
Share

It's funny, but only now I understand what you wanted to do all along.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How To Destroy A Clone? 2 Answers

Destroying clones of a prefab from another script 3 Answers

all objects sharing script get destroyed? 3 Answers

How to Destroy the instances of 2 gameObjects at the same time? 1 Answer

Delete multiple clones 2 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