Search Unity

Co-routines?

Discussion in 'Scripting' started by AaronC, Aug 3, 2006.

  1. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Hello again! :p

    I have two generic scripts, that work if I only use one at a time but if I apply both to my game object, only one will work(the bottom one. If I combine the two, only the bottom one works.. Any tips? Is this a "co-routine" that needs to be handled in a special way? Try it-It doesnt work trust me...
    Code (csharp):
    1.  
    2. function OnCollisionEnter()
    3. {
    4.     Invoke("LoadNextLevel", 2.0);
    5. }
    6.  
    7. function LoadNextLevel()
    8. {
    9.     Application.LoadLevel("AtlantisAug2");
    10. }
    Second Code(Instantiate)
    Code (csharp):
    1. var subDead : Transform;
    2. function OnCollisionEnter ()
    3.  
    4. {
    5.    Destroy (gameObject);
    6.    Instantiate (subDead, transform.position, transform.rotation); }
    7.  
    Combined 1
    Code (csharp):
    1.  
    2. var subDead : Transform;
    3. function OnCollisionEnter ()
    4.  
    5. {
    6.    Destroy (gameObject);
    7.    Instantiate (subDead, transform.position, transform.rotation); }
    8. {
    9.     Invoke("LoadNextLevel", 2.0);
    10. }
    11.  
    12. function LoadNextLevel()
    13. {
    14.     Application.LoadLevel("AtlantisAug2");
    15. }
    (Only instantiate script works) And finally, combined 2
    Code (csharp):
    1. var subDead : Transform;
    2. function OnCollisionEnter ()
    3. {
    4.     Invoke("LoadNextLevel", 2.0);
    5. }
    6.  
    7. function LoadNextLevel()
    8. {
    9.     Application.LoadLevel("AtlantisAug2");
    10. }
    11. {
    12.    Destroy (gameObject);
    13.    Instantiate (subDead, transform.position, transform.rotation); }
    14.  
    Hope the question is straightforward enough. Thank You!
    AC
     
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    When you Destroy() the gameObject, the scripts aren't going to be running anymore two seconds later.

    -Jon
     
  3. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    It seems you have some errors in your usage of braces {}

    Basically braces mark a "block" of code (such as a function.

    Braces need to be balanced. No { { } Use {} or { { } }

    If you want to combine functions, this is the principle:

    Code (csharp):
    1.  
    2. function part1 () {
    3.    code1 ();
    4. }
    5.  
    6. function part2 () {
    7.    code2 ();
    8. }
    9.  
    Becomes:

    Code (csharp):
    1.  
    2. function part1AndPart2 () {
    3.    code1 ();
    4.    code2 ();
    5. }
    6.  
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yep. :) For one, keep your use of braces { } consistent, because it makes it easier to follow what's going on. Your braces actually ARE balanced, but I thought they weren't at first glance too. However, they're still not being used correctly, as I'll explain later.

    When you use the two scripts on the same object, the second one destroys GameObject when a collision occurs, so the first script isn't around any more to do anything.

    This isn't going to work for two reasons. Only the Destroy and Instantiate lines are part of the OnCollisionEnter function. You have the Invoke line off by itself, not a good thing. I'm thinking you wanted those three lines to be part of the OnCollisionEnter function, so you should just have one set of braces surrounding them all.

    However, that's still not going to work, because the GameObject is destroyed by the next frame. It's not going to be still there 2 seconds later, so the rest of the script won't be around to execute. The Invoke line would start the 2 second timer, but the next frame, the script is destroyed, so nothing else happens.

    This shouldn't work...note that you get a compile error. You have the Destroy and Instantiate lines by themselves, not part of any function. If it does work, well, I have no idea why. It certainly doesn't work here.

    The way to do this is to use two scripts, one on the sub, and one on subDead. Put this script on the sub:

    Code (csharp):
    1.  
    2. var subDead : Transform;
    3.  
    4. function OnCollisionEnter ()
    5. {
    6.    Destroy (gameObject);
    7.    Instantiate (subDead, transform.position, transform.rotation);
    8. }
    9.  
    And put this script on object you're using for subDead:

    Code (csharp):
    1.  
    2. yield WaitForSeconds(2);
    3. Application.LoadLevel("AtlantisAug2");
    4.  
    That way, subDead is instantiated with the second script attached, so it will do the waiting around thing after GameObject is gone.

    --Eric