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 AtomicMarine · Nov 21, 2011 at 06:46 PM · gameobjectiphonedestroyprefabsinstantiated

Deleting instantiated prefabs clones?

I need to delete an instated gameObjects clone once I instate it again, for example if a person were to place a block, the next time that person would place that same block the original would become destroyed. How would one do this?? what is the theory behind the code?

I've tried using static variables and some very complex methods to not much avail.

Comment
Add comment · Show 3
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 kevork · Nov 21, 2011 at 07:17 PM 2
Share

Instantiating and destroying objects is fairly expensive, why not just use the same object again?

avatar image Anxo · Nov 21, 2011 at 07:19 PM 0
Share

That is a good point, you could just have it flying somewhere and then he places the box just give it a new position.

avatar image AtomicMarine · Nov 21, 2011 at 07:24 PM 0
Share

hmm that could do, but I would have to change my code around quite a bit because I'de have to instantiate it the first time :P oh well worth a shot >.<

2 Replies

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

Answer by Anxo · Nov 21, 2011 at 06:54 PM

When you instantiate the first block give it a a name. Then when you instantiate the second block, check if that name exists, if so delete it and assign the second block to the name.

 function PlaceBlock(){
 
 var clone : GameObject;
 if(clone!= null){
 Destroy(clone);
 clone = instantiate.........
 
 }
 else
 clone = instantiate.........
 }

this will give you errors but you get the idea right?

Comment
Add comment · Show 7 · 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 AtomicMarine · Nov 21, 2011 at 06:59 PM 0
Share

Thanks man, ill try it out, i get the idea behind it. I'm not instantiating my game Object in a function but I think I will move it to one :P ill thumbs up and choose this answer if it works ^^

avatar image AtomicMarine · Nov 21, 2011 at 07:20 PM 0
Share

Hey I got the script working but it's not deleting the clone when there are other clones in play....?

avatar image Anxo · Nov 21, 2011 at 07:23 PM 0
Share

my code is not correct I was just trying to give the idea, clone in destroy for one needs to be clone.gameObject and so on

avatar image AtomicMarine · Nov 21, 2011 at 07:26 PM 0
Share

I know, I did change those things I'm gonna try tagging the clone and destroying it right before the other one instantiates >.<

avatar image AtomicMarine · Nov 21, 2011 at 07:28 PM 0
Share

last attempt worked perfectly! what I did was I took the concept of tagging the box from your previous answer and destroyed the tagged version right before I instantiated it in it's latest form :) thanks for all your help Anxo, it was both your answers combined that got this one solved ^^.

Show more comments
avatar image
1

Answer by Anxo · Nov 21, 2011 at 07:05 PM

Anxo's answer is one way to do it. Here is another.

You can give the box a tag, like, "box". Then you create a new script and place it on the box that looks for boxes at the beginning and destroys it.

Like this.

 function Awake(){
      var oldBox : GameObject = GameObject.FindWithTag("box");
      if(oldBox)
      {
           Destroy(oldBox.gameObject);
      }
 
 
 } 

Then every time a new box comes in the scene, it will look for the old box and if there is one it destroys it.

Comment
Add comment · Show 2 · 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 AtomicMarine · Nov 21, 2011 at 07:21 PM 0
Share

I tried this method as well, it just resulted in me not being able to have any game objects tagged with "box" in play :/

avatar image Anxo · Nov 21, 2011 at 07:24 PM 0
Share

ah yea that makes sense. because It is taged box, remove the tag and add the line gameObject.tag = box at the end of the function, that should fix that.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Is it Possible to Bring back a Destroyed GameObject? 3 Answers

Ai should delete a build turret 1 Answer

Destroy all Objects of a Type in Game 1 Answer

Trash Collection For Instantiated Game Object 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