(SOLVED) Destroy(gameObject) doesn't destroy the gameObject, only disables behaviours.
Hello friends. I'm in the midst of prototyping a game, and I've run into a problem that makes zero sense to me, even after a few hours of working on it.
Calling Destroy(gameObject) on the object seems to disable every single behavior on it, but doesn't destroy the actual object, almost as if some internal exception occurs just before removing the object from the scene.
I am using a custom-built content loading system which may be the culprit, but it is only responsible for loading and instantiating objects, not destroying them. Either way, I've scoured it for any possible related issues but have been unable to find any.
Additionally, which might be related as it started occurring about the same time: Some of my behaviors don't seem to be running coroutines or methods invoked by Invoke() at all despite being active and enabled. Far as I can tell, this is exclusive to objects instantiated by my content system, but I can't find any reason why this is the case, nor any internal pattern to it. Best I got is that the objects are instantiated based on objects in the scene that are disabled, which I use as a cache.
For instance, it happens to objects created through this instantiate method:
public GameObject Instantiate()
{
GameObject go = UnityEngine.Object.Instantiate(GetCache());
go.SetActive(true);
return go;
}
private GameObject GetCache ()
{
if (_cache == null)
{
_cache = Content.Get(Path, typeof(GameObject)) as GameObject;
_cache.SetActive(false);
}
return _cache;
}
Content.Get(...) in this case is equivalent to Resources.Load(...)
The GitHub repository, should it be of use.
EDIT
I've done some more troubleshooting and found where the issue originates and thus been able to work around it. It was caused by an Invoke() inside an Awake() function on the behaviour that moves my enemy characters. This Invoke() would be called just before the object was disabled within the same frame, which may have caused some internal Unity bug that caused all Invokes and Coroutines to halt. I'll report the bug to Unity.
Your answer
Follow this Question
Related Questions
Invoke Random function 0 Answers
How to change the bullet fire rate in C#? 1 Answer
Deleting objects at specific position 1 Answer