Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by instruct9r · Aug 04, 2014 at 10:34 AM · objectdestroyfunctionyieldstops

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...

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Aug 04, 2014 at 04:03 PM 0
Share

Please post the BackToCheckPoint() coroutine and the function in the bullet that calls the routine.

avatar image felixpk · Aug 04, 2014 at 04:10 PM 0
Share

That's right, that should not be happening. Just for testing, try gameObject.Send$$anonymous$$essage("BackToCheckpoint");

avatar image instruct9r · Aug 04, 2014 at 05:17 PM 0
Share

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....

avatar image robertbu · Aug 04, 2014 at 05:36 PM 2
Share

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();
 }
avatar image Eric5h5 robertbu · Aug 04, 2014 at 05:47 PM 1
Share

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.

avatar image instruct9r · Aug 04, 2014 at 07:39 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image instruct9r · Aug 04, 2014 at 08:59 PM 0
Share

$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Make an object destroy the object it is touching? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges