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 Irsan · May 01, 2011 at 04:47 AM · destroyclonesall

Destroy all previous clones.

I want only one prefab to be in the scene at once. Each time I make a new one ingame, I want the previous one to be destroyed. How do I get it to check the number of clones in the scene, and only keep the one that was created sooner?

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

2 Replies

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

Answer by Joshua · May 01, 2011 at 05:07 AM

I think this would work. I wrote it so you can have x many clones and if you have one more the first one get's destroyed. In your case this maximum number is one.

var maxNumberOfClones : int;

var location : Vector3; var prefab : GameObject;

private var numberOfClose : int; private var activeClones : Array = new Array ();

function Update () {

 if (some_condition) { //when do you want to instantiate

     activeClones.Add(Instantiate(prefab,location,Quaternion.identity); //at the end of the array, add the newest clone

     if(activeClones.length > maxNumberOfClones) { //if there are now to many clones

         Destroy(activeClones[0]) //destroy the first entry of the array

         activeClones.Shift(); // remove this entry from the array    
     }

 }

}

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 Irsan · May 01, 2011 at 05:11 AM 0
Share

This goes on the prefab, right? And I'm guessing I need to fill in some of those variables.

avatar image Joshua · May 01, 2011 at 05:20 AM 0
Share

No, this goes anywhere but those prefabs, because those will get destroyed and you only want one version of this script to run at a time. Just put it on your main camera or something else you won't destroy. And yes, you have to fill in the blanks.

avatar image Irsan · May 08, 2011 at 02:49 AM 0
Share

I don't understand what to put in the if statement, or what I'm supposed to do with the "location".

avatar image Joshua · May 08, 2011 at 03:40 AM 0
Share

When do you want to instantiate? Leave the whole if statement out to instantiate every frame. But put, for instance, Input.Get$$anonymous$$ouseButtonDown(0) in there to only instantiate when you click your mouse. It's just an empty placeholder :)

avatar image Irsan · May 08, 2011 at 04:24 AM 0
Share

That's exactly what I did. It's not working, though.

Show more comments
avatar image
2

Answer by Eric5h5 · May 01, 2011 at 05:45 AM

Keep a reference to the clone when you instantiate it.

var prefab : GameObject; private var clone : GameObject;

function Spawn () { if (clone) { Destroy(clone); } clone = Instantiate(prefab); }

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 Joshua · May 01, 2011 at 05:58 AM 1
Share

Damn you for your simplicity! But my script works for any maximum number of clones ^.^ so hah :p

avatar image Eric5h5 · May 01, 2011 at 06:31 AM 1
Share

Well, I thought maybe I'd just answer the question. ;)

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

No one has followed this question yet.

Related Questions

Destroy All Objects In The Scene??? 1 Answer

Destroyed clones freezing but not disappearing? 1 Answer

How do I destroy a specific clone of a GameObject? 1 Answer

Ignoring collisions with clones 3 Answers

Tagged Clones not destroyed by script 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