- Home /
When an object is destroyed?
Is there a way to trigger a function when an object is destroyed(by another object's script)? Thanks.
Answer by Jesse Anders · Nov 17, 2010 at 05:37 AM
The need for an OnDestroy() message has come up in the forums in the past. It seems logical that there'd be such a function, but for whatever reason it's never been implemented.
You can use OnDisable() for this, but you have to keep in mind that OnDisable() is called in circumstances other than an object being destroyed via the Destroy() function, and code accordingly.
My own preferred solution (and one that I've seen mentioned elsewhere on the forums as well) is to use a custom Destroy() function that first sends an OnDestroy() message and then destroys the object. This isn't ideal in that you have to remember to use the custom function when destroying an object (provided you want the OnDestroy() message to be sent, at least). But, I've found it to be a suitable solution.
OnDestroy() message seems quite implemented to me.. of course if we are talking about $$anonymous$$onoBehaviour scripts
Answer by Yoerick · Nov 17, 2010 at 05:25 AM
You could try using a static bool, which is accessible in another script.
script1
public static bool objectDestroyed = false;
if(/*some statement*/)
{
Destroy(/*your object*/);
objectDestroyed = true;
}
script2
if(script1.objectDestroyed)
//do something
This cannot be used as the OP wanted since all instances of your script1 would be considered as "objectDestroyed" because of usage of static.
But how could you put If(object is destroyed) ? What should i put between () ?
This question is eight years old, the OnDestroy() message has long been here. But to answer your question, for UnityEngine.Object derived types, all you have to do is if(objectInstance)
as it has an implicit cast to bool that returns false when the object was destroyed
Answer by denewbie · Nov 17, 2010 at 08:53 AM
Based on how I see it you just want to trigger another script when an object is being destroyed. Based on the circumstances what I think would work best for most cases would be to call get a script in the object to called the other script instead.
In that way, you could save resources on the fact of having use the update script as a listener for when the object is to be destroyed.
Placing a Boolean might not be idea because it never indicates whether or not the object was ever created in the first place and might trigger your script event immediately. IF we're talking about multiple instantiated objects, there's no telling how many Boolean variables we'll need to declare. What I normally do for multiple objects checking would be to create a hashtable or a List object and store all my objects or object names inside.
This is in C#:
List<string> ObjectNames = new List<string>()
void Start(){ // Load your object names ObjectNames.Add("Names1"); ObjectNames.Add("Names2"); ObjectNames.Add("Names3"); ObjectNames.Add("Names4"); . . . . ObjectNames.Add("NamesN"); }
I'll then loop through the list and check if the gameObject is null:
GameObject ActiveObj = null;
void Update(){ foreach (string objNm in ObjectNames) { ActiveObj = GameObject.Find(objNm); if ( !ActiveObj ) // This checks if the object is null { myFunction(); // Run this function of the object is null because it means the object is destroyed ObjectNames.Remove(objNm); // Remove the object from the list } } }
In this manner you can have almost an unlimited number of objects in your list. You can also add and remove objects from the list as they are being created or destroyed.
What advantage does this have over the message-based solution I described? It seems what you described above would be far more complicated to implement and far less efficient than the message-based solution, while not offering any real advantages to speak of.
Is keeping your object table in containers a lot of upkeep work or do you autoupdate it ?