Search Unity

Smashing Object Delay

Discussion in 'Scripting' started by DarkNeo, Jun 8, 2015.

  1. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Hi there,

    I am trying to add a slight delay to my brick destroying script. It works fine, But adding a slight delay to it I believe it will work perfect for what I am trying to achieve.

    Code (CSharp):
    1. public class Brick2D : Platform2D {
    2.    
    3.     public ParticleSystem particles;
    4.     public BoxCollider2D myCollider;
    5.     public MeshRenderer myRenderer;
    6.  
    7.    
    8.     override public void DoAction(RaycastCollider2D collider, RaycastCharacterController2D character)
    9.  
    10.     {
    11.  
    12.         // Hitting from below
    13.         if (collider.direction == RC_Direction.DOWN)
    14.         {
    15.             if (particles != null) particles.Play();
    16.             myCollider.enabled = false;
    17.             myRenderer.enabled = false;
    18.        
    19.  
    20.  
    21.         }
    22.     }
    23. }
    in Javascript its just a matter of adding in this.

    Code (JavaScript):
    1. yield WaitForSeconds(2.0);
    I understand in C# that yield is written with return new, but also it's supposed to be written with
    IEnumerator/Coroutines as well. I am not sure how to tie this all in together. I have been playing with it a fair bit now. Still no luck, could anyone point me in the right direction?

    Thank you :)
     
  2. magnetix

    magnetix

    Joined:
    Apr 4, 2010
    Posts:
    102
  3. magnetix

    magnetix

    Joined:
    Apr 4, 2010
    Posts:
    102
    Ok, sorry I was too hasty - it looks like you want to preserve the object but just do some delayed processing. So just see this page it shows the C# usage of yield. Make sure you click the little c# button at the top right.

    You have to make the coroutine method an IEnumerator, then use "yield return new WaitForSeconds(x);"
     
    DarkNeo likes this.
  4. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Cheers, yeah I need to play around with that more.