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 Triph · May 04, 2013 at 09:27 PM · c#instantiatedestroy

How to determine and delete the oldest instantiated object?

I am new to unity and programming.

I am creating a waterfall of letters that is destroyed upon typing the corresponding letter. But if there are multiple instances of a letter, every letter of that type is destroyed. How can i track and delete only the oldest one?

I am not looking for complete code, just a point in the right direction, I have looked into List and arrays but I can't wrap my head around how to.

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
0

Answer by Bunny83 · May 04, 2013 at 10:53 PM

Well, it's pretty easy. Basically there are two ways:

  • time-based (so each object lives for a certain amount of time eg. 5sec)

  • count-based (there are only a certain number of objects allowed.)

For a lifetime limitation all you need to do is call Destroy right after you Instantiated the object but make sure you provide a timeout value:

 // C#
 float timeToLive = 5.0f;
 
 public Transform CreateObject()
 {
     Transform clone = (Transform)Instantiate(prefab);
     Destroy(clone.gameObject, timeToLive);
     return clone;
 }

For a count limitation, just add your objects to a List and check if the list count is larger than your desired threshold and simply destroy the first object in the list (and remove it) until the count is within the desired limit:

 //C#
 int maxObjCount = 20;
 List<Transform> objs = new List<Transform>();
 
 public Transform CreateObject()
 {
     Transform clone = (Transform)Instantiate(prefab);
     objs.Add(clone);
     while(objs.Count > maxObjCount)
     {
         if(objs[0] != null)
             Destroy(objs[0].gameObject);
         objs.RemoveAt(0);
     }
     return clone;
 }

edit
Ok, if you have a stream of different letters and want to destroy the oldest letter "X" there are also several possibilities. One way is to hold a List for each "letter type" in a dictionary for example. That way you can easily access the "correct list" for a certain letter. In this case you need to put new letters in the "correct" list.

Another way would be to have only one list which makes the bookkeeping of the objects simpler but the searching a bit more complicated. This is probbly the easiest way. Even with 1000 letters on screen it's no problem too search through 1000 objects since you only do this when the user presses a button.

To identify the letter-type of an object you could simply rename the cloned object so it's name reflects it's letter:

 // To create an object
 // C#
 void CreateLetter(string aLetter)
 {
     // If you have seperate prefabs you have to select the right one for "aLetter" here
     Transform clone = (Transform)Instantiate(prefab);
     clone.name = aLetter;
     objs.Add(clone);
 }
 
 // To remove the first letter
 // C#
 void RemoveLetter(string aLetter)
 {
     for(int i = 0; i < objs.Count; i++)
     {
         if (objs[i].name == aLetter)
         {
             Destroy(objs[i].gameObject);
             objs.RemoveAt(i);
             break;                      // This is important to stop the loop once the first is found
         }
     }
 }

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 Triph · May 05, 2013 at 08:53 AM 0
Share

I have implemented a lifetime limit already. $$anonymous$$y issue is that when there are multiple objects of the letter B, a B keypress deletes all of them, i want them to be deleted in sequence oldest to youngest.

I have numbered the copies, but say I iterate through the numerical of "letterA01" and delete the first I find. Wouldnt this be terribly expensive when i hit 1000 spawned and have to search through all those no-existing ones?

avatar image Bunny83 · May 05, 2013 at 09:14 AM 0
Share

How is the letter destroyed? Is there a script on the letter object that destroys itself, or do you have a dedicated script that handles that?

I'll edit my answer...

avatar image
0

Answer by armoredpokey · May 05, 2013 at 12:04 PM

Well, if you're concerned about performance, you could create a hash map of letter names => ArrayLists of letters.

 var letters : Hashtable = new Hashtable();
 
 function Awake() {
   alphabet = ["a", "b", "c", "d" ... etc ];
   for (letter in alphabet) {
     letters[letter] = new ArrayList();
   }
 }

Then, the approach is basically the same as the answer above; whenever you add a letter to the scene, make sure you update the arraylist corresponding to the letter, and whenever you want to remove one, just find that arraylist and delete its first element.

 function AddLetter(l) {
    // do something to create a game object
    letters[l].Add(object);
 }

 function RemoveLetter(l) {
   if (letters[l].Length == 0) return;
   object = letters[l][0];  // you might need to do some typecasting for C#, like "object = (GameObject) letters[l][0]"
   letters[l].RemoveAt(0);
   Destroy(object);
 }
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

16 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

Related Questions

Destorying prefab and instanstiating again? 0 Answers

instantiate object 2 Answers

Removing instantiated objects?? 1 Answer

Show/hide object in C# 2 Answers

How do I destroy a Instantiated UI image that is pushed on the Canvas? 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