- Home /
How to remove the oldest object
I want to make a tactical insertion and I want to only allow one tact in the scene so that the player can spawn in one place instead of bugging my game out. So how do I make my objects like have a index number or whatever? And I'd like any script-related answers in Javascript, thank you.
I absolutely can't understand your scenario, but as for the question on how to index your objects, simply use a list, and whenever you spawn an object put it in the list. Then the first object in the list is the latest, so you can remove it from the list and destroy it or whatever.
Not big on Javascript, but I'm sure some simple searches will get you to tutorials for using lists in javascript.
If you want the oldest why not use Time.time? Set a timeCreated variable in Start() and check that.
yes your description is very unclear but based on the title alone go with what Huacanacha said, give everything a float called birthTime or something and compare that number to see who is older. Lowest number wins
Ok, but how do I check for when a object was spawned with Time.time?
If you create these spawned objects in the same place, keep a list of them there and use the First-In-First-Out queue that tbkn describes. If they can come from multiple sources, add a timeSpawned variable to the spawned object script and set it to Time.time in the Start() function. When you collect your spawned objects to check which one is oldest just compare the timeSpawned variable on each.
I don't use UnityScript/Javascript so I'm not the best person to give you code examples, but it's very straight forward if you know the language.
How do you create and store the spawned objects? If you include some of your code I may be able to tell you where to do what.
Your answer
Follow this Question
Related Questions
Enable Instantiated Object 1 Answer
Instantiate with random scriptableObject 2 Answers
Need help moving player object towards an instantiated ball 1 Answer
Move a object to certain cordinates 2 Answers