In Unity I frequently find myself doing this in C#:
GameObject myObject = new GameObject();
try{
// do something with that object
}
finally{
Destroy(myObject);
}
A good example is creating a temporary pivot point for another object. I create the object, parent whatever object I want to rotate, do the rotation, then destroy the object. It's in a finally
block so in case anything goes wrong it still destroys the object.
Does Unity have any elegant way to do this? Writing this all the time is tedious, and it's easy to forget the Destroy
method at the end.