- Home /
Obj calls function, on another obj and get's destroyed. Function stops
Hello..
I have found an interesting issue with my project.
Imagine a bullet hits your character and then get's destroyed. When it his the player, it calls a function... BackToCheckPoint();
The character have to go back to checkPoint position. So the function BackToCheckPoint(), that was called from the bullet is executing several lines of code.
The problem comes, if there is a "yield" in the function (BackToCheckPoint). If the bullet get's destroyed, before the yield finishes, the function stops the execution at the yield.
So my question is: If one object calls a function on another object and get destroyed, why does that function is dependable from the object, that is calling it? It is allready called, so it shoud not be watching, what's happening to the object, that called it...
Please post the BackToCheckPoint() coroutine and the function in the bullet that calls the routine.
That's right, that should not be happening. Just for testing, try gameObject.Send$$anonymous$$essage("BackToCheckpoint");
robertbu, no need to post anything. You can make 2 simple objects and make one call a function on the other and then get's destroyed. Add 2 prints in the function, separated by yield...
This is the code, that i am using and you can use...
function OnTriggerEnter(col : Collider)
{
if (col.tag == Tags.player) // If Player triggers
{
col.transform.GetComponent(Player$$anonymous$$ovement_JS).BackToCheckpoint(); // $$anonymous$$ake the player die and get back to last checkPoint
Destroy(trans.gameObject);
}
}
The workaround, that i found is that ins$$anonymous$$d of calling BackToCheckPoint, i made the bullets to call another function, which looks like this:
function CallBackToCheckpoint()
{
BackToCheckPoint();
}
This way i am sure, that the BackToCheckPoint will be executed, cuz it is getting called from the same script. This method works.
I just don't think that, when object calls a function and get's destroyed, the function shoud stop....
You are right about the behavior. I don't know if this intended behavior, or if this is a Unity bug. It may be (even likely) that any Coroutines started by an object are stopped when the object is destoryed. Anyway, there is a simple workaround. $$anonymous$$ake BackToCheckpoint() a function (no yield), that then calls the coroutine. Something like:
function BackToCheckpoint() {
_BackToCheckpoint();
}
It used to be unnecessary to do this in earlier versions (3.x definitely, not sure when it changed exactly). So it does seem somewhat bug-like.
Yep, that's what i was using.
Anyway i still don't have Unity 4.5 installed (Working on 4.3). Does it works the same in 4.5 as well?
Answer by rutter · Aug 04, 2014 at 08:12 PM
I see two design quirks coming together.
First, my experience in 4.x is that coroutines are always owned by a particular MonoBehaviour, and will stop executing if that owner is ever destroyed, deactivated, or disabled.
Second, UnityScript handles coroutines such that "they just work", unlike C# where you explicitly call StartCoroutine
. This means you don't necessarily know which MonoBehaviour ends up owning the coroutine -- the caller, or the callee?
Given the problems mentioned above, it looks like the caller ends up owning the coroutine. If the caller is destroyed, the coroutine is destroyed with it.
In C#, you could fix this by having the other behavior call StartCoroutine
. In UnityScript, it looks like you'd have to create a "hook" function, such as your CallBackToCheckpoint
; because the other behavior calls the coroutine, it ends up owning it.
In either language, it's also possible to fix this by having a third object own the coroutine.
$$anonymous$$eaning it's not a bug, jus the way that Unity works...
Thanks for the explanation :)
I'll mark is as solved, cuz the workaround is known :). Just wanted to discuss, that weird (in my opinion) behaviour with you guys... But maybe my logic is wrong...
Your answer
Follow this Question
Related Questions
How to destroy a unit contained in multiple lists 1 Answer
Dispose Object in IOS 0 Answers
Respawn after delay 3 Answers
Coroutine a function within a loop? 1 Answer