How do I delete objects in an animation in a certain order?
So I am making an assembly line and on the line are objects, moving at a certain speed in the animation. There are more than 40 objects on the line and the line is kind of long, so I didn't want to animate every object trough the whole line, but instead, because the objects are identical, i animated each object to the place where the object in front of it was placed in the start and then the animation loops. (If you don't see what I mean, please look at the picture) The problem with this is that the number of objects on the line remain the same, but to look like they are moving, I need to delete the first object in the line after each loop. With the code below, I can do that, but the animation doesn't delete the objects in the same order. It deletes the objects in the middle of the line instead, and before every object is deleted, the first and the last one remain.
The objects are named "pillBottleModel (number)", where the number is the order in which the bottles are placed from the start of the line.
void Update()
{
if(anim.enabled == true && anim.GetBool("sensor") == true)
{
//find the first object
child = parent.transform.Find("pillBottleModel ("+ i +")");
Destroy(child.gameObject, timeLoop * (i+1));
if (i < NumOfBottles)
{
i++;
}
}
}
EDIT: Basically, my question is "I have an animation where objects move forward in a straight line for a bit and then they stop. How do I delete the first object in the line after every animation loop ?"
You can use animation events for that. create an animation event at the last frame of the animation. for refference
Answer by ashitpie · Sep 04, 2019 at 11:10 AM
I solved it! The problem was that as I deleted the first model, the other models got mixed up, so they weren't in the same order anymore... Instead of deleting the objects, I just set them to invisible with "GameObject.SetActive(false)"...