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 lampshade · Jan 15, 2011 at 02:09 AM · oncollisionenterwaitforsecondsstartcoroutine

Invoke a Coroutine From OnCollisionEnter

Hi, it seems that the 'yield return StartCoroutine' in OCE does not act properly when being invoked from the OnCollisionEnter() Method:

public class example : MonoBehaviour {

  public GameObject instPrefab;
  public Effects e;
  public void Start() {e = Effects.CreateInstance<Effects>();}
  public IEnumerator OnCollisionEnter(Collision col) {
      Debug.Log("Collided!"); //<--happens
      ContactPoint cp = col.contacts[0];
      Vector3 pos = cp.point;
      if (instPrefab) {
           yield return StartCoroutine(e.ParticleEffects(instPrefab, pos));
           Debug.Log("Done with Coroutine"); //Never happens
      }
  }

}

public class Effects : ScriptableObject { public IEnumerator ParticleEffects(GameObject instPrefab, Vector3 pos) {

     Debug.Log("Starting Co-Routine"); // <-Happens
     GameObject inst = (GameObject) Instantiate(instPrefab, pos, Quaternion.identity);
     ParticleEmitter pE = inst.GetComponent<ParticleEmitter>( );
     while (pE.maxSize > 0) {
         pE.maxSize -= 5 * Time.deltaTime;
         yield return null; // return a null frame?
     }
     Debug.Log("End");   // <-- Still Does not happen
 } 

}

Is their something that I am missing? Or am I just unable to do this after the method OCE is invoked? Again, the problem $is not being able to Wait for a desired amount of seconds in the Coroutine, "ParticleEffects"

Comment
Add comment · Show 10
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 lampshade · Jan 15, 2011 at 02:23 AM 0
Share

I suppose the question may now be, what is attached to what?

avatar image Bunny83 · Jan 15, 2011 at 04:08 AM 1
Share

where do you set your "instPrefab" ? because it's private... (should be public?) are you even sure that you reach the yield statement? Debug.Log before this line, maybe there went something else wrong.

avatar image Bunny83 · Jan 15, 2011 at 04:19 AM 1
Share

ehm, did you write this example without testing? Because ContactPoint don't have .contacts[] only .point. And you should make a block after "if (instPrefab)" and include your Debug.Log to see if your prefab is set properly. And the Effects class needs one more "}"

avatar image Bunny83 · Jan 15, 2011 at 04:35 AM 1
Share

:D np, I've tried your code and it works... don't know what's wrong. Do you even get a collision? ps. You changed the code in Particle Effects() but you removed the inst before GetComponent ;) Just a little hint: if nothing works just put after every single line a Debug.Log and watch carefully what is executet and what is not

avatar image Bunny83 · Jan 15, 2011 at 05:14 AM 1
Share

Do you use 2 seperate files for the "example" script and your "Effects" script? Because a ScriptableObject have to be in it's own file (filename equals classname). Try to put a Debug.Log after each line of code and watch how far you come.

Show more comments

1 Reply

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

Answer by Eric5h5 · Jan 15, 2011 at 02:52 AM

OnCollisionEnter itself can be a coroutine; you don't need to call another coroutine from it.

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 lampshade · Jan 15, 2011 at 03:36 AM 0
Share

no idea what its not working =/

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

No one has followed this question yet.

Related Questions

WaitForSeconds problem with Unity Pro 3 Answers

How could i convert this from java to #c 1 Answer

Scrolling Text StartCoroutine not working C# 2 Answers

How to make a 3d text writeOut method? 1 Answer

OnCollisionEnter as IEnumerator problem (WaitForSecond) 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