Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trying to make a delay

Discussion in 'Scripting' started by GreenBoxInteractive, Apr 1, 2015.

  1. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Hello. I am using this C# script to try to move a sprite one way, wait a few seconds then move it back the other way, I am stuck :((

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveScript : MonoBehaviour {
    5.    
    6.     public float speed;
    7.     int delay = 6;
    8.  
    9.     IEnumerator Start()
    10.         {
    11.         transform.Translate (Vector3.right * Time.deltaTime * speed);
    12.         yield return new WaitForSeconds(delay);
    13.         transform.Translate(Vector3.left * Time.deltaTime * speed);
    14.         }
    15.        
    16.     }
    17.    
     
  2. PeppyZee

    PeppyZee

    Joined:
    Apr 1, 2015
    Posts:
    20
    I am not sure if this will work, but I think you have to call the IEnumerator Coroutine.


    Try:

    void Start()
    {
    StartCoroutine("Start");
    }
     
  3. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Nope, that just crashed Unity Editor :S
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    give your IEnumerator a new method name that isnt Start and call it from your start method using StartCoroutine
     
  5. PeppyZee

    PeppyZee

    Joined:
    Apr 1, 2015
    Posts:
    20
    Humm what about Renaming your Coroutine:

    Example:

    void Start()
    {
    StartCoroutine("MyStart");
    }

    IEnumerator MyStart()
    {
    transform.Translate (Vector3.right * Time.deltaTime * speed);
    yield return new WaitForSeconds(delay);
    transform.Translate(Vector3.left * Time.deltaTime * speed);
    }
    }
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Start can be a coroutine so renaming and all that jazz is redundant.

    Original code should work. Log Time.time before and after the yield statement.
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Probably not the best idea to start a co-routine called start from start though...
     
  8. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135


    My debug log. With this code. The object doesn't move

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CloudMove : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     int delay = 6;
    8.  
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.         StartCoroutine("MyStart");
    13.     }
    14.  
    15.     IEnumerator MyStart()
    16.     {
    17.         transform.Translate (Vector3.right * Time.deltaTime * speed);
    18.         Debug.Log (Time.time);
    19.         yield return new WaitForSeconds(delay);
    20.         Debug.Log (Time.time);
    21.         transform.Translate(Vector3.left * Time.deltaTime * speed);
    22.     }
    23. }
     
  9. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    If you want it to be a permanent back and fourth you could do this:

    Code (CSharp):
    1.  
    2. public class MoveScript : MonoBehaviour{
    3.      public float speed;
    4.      public float delay;
    5.      bool goLeft;
    6.  
    7.      void Start(){
    8.           goLeft = true;
    9.           Invoke("SwitchMove", delay);
    10.      }
    11.  
    12.      void Update(){
    13.           if(goLeft){
    14.                transform.Translate(Vector3.right * Time.deltaTime * speed);
    15.           }else{
    16.                transform.Translate(Vector3.left * Time.deltaTime * speed);
    17.           }
    18.      }
    19.  
    20.      void SwitchMove(){
    21.           if(goLeft){
    22.                goLeft = false;
    23.           }else{
    24.                goLeft = true;
    25.           }
    26.  
    27.           Invoke("SwitchMove", delay);
    28.      }
    29. }
    30.  
     
    GreenBoxInteractive likes this.
  10. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    It works! Is there a way I can make the speed a random number between 1 and 3?
     
  11. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    public float minSpeed;
    public float maxSpeed;

    float curSpeed;

    In the update replace speed with curSpeed;

    In the SwitchMove() under goLeft = true add the line: curSpeed = Random.range(minSpeed, maxSpeed)
     
  12. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    `UnityEngine.Random' does not contain a definition for `range'

    Scratch that, fixed it: here is the finished script for anyone who might want it. Thank you very much everyone


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CloudMove : MonoBehaviour{
    5.     public float speed;
    6.     public float delay;
    7.  
    8.     public float minSpeed;
    9.     public float maxSpeed;
    10.    
    11.     float curSpeed;
    12.  
    13.     bool goLeft;
    14.    
    15.     void Start(){
    16.         goLeft = true;
    17.         Invoke("SwitchMove", delay);
    18.     }
    19.    
    20.     void Update(){
    21.         if(goLeft){
    22.             transform.Translate(Vector3.right * Time.deltaTime * curSpeed);
    23.         }else{
    24.             transform.Translate(Vector3.left * Time.deltaTime * speed);
    25.         }
    26.     }
    27.    
    28.     void SwitchMove(){
    29.         if(goLeft){
    30.             goLeft = false;
    31.         }else{
    32.             goLeft = true;
    33.             curSpeed = Random.Range(minSpeed, maxSpeed);
    34.         }
    35.        
    36.         Invoke("SwitchMove", delay);
    37.     }
    38. }
     
  13. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    it may be misspelt type Random. and look for something that looks like range

    Edit:

    Oh in the Start() function add the line Random.seed = Time.time
     
  14. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    You can get rid of public float speed if you change the second transform.translate to use the curSpeed aswell
     
    GreenBoxInteractive likes this.
  15. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)
     
  16. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Random.seed = (int)Time.time
     
  17. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    My object is now glitching sideways and not moving
     
  18. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Can you post the script so i can see if i can spot the issue
     
  19. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CloudMove : MonoBehaviour{
    5.  
    6.     public float delay;
    7.  
    8.     public float minSpeed;
    9.     public float maxSpeed;
    10.    
    11.     float curSpeed;
    12.  
    13.     bool goLeft;
    14.    
    15.     void Start(){
    16.         Random.seed = (int)Time.time;
    17.         goLeft = true;
    18.         Invoke("SwitchMove", delay);
    19.  
    20.     }
    21.    
    22.     void Update(){
    23.         if(goLeft){
    24.             transform.Translate(Vector3.right * Time.deltaTime * curSpeed);
    25.         }else{
    26.             transform.Translate(Vector3.left * Time.deltaTime * curSpeed);
    27.         }
    28.     }
    29.    
    30.     void SwitchMove(){
    31.         if(goLeft){
    32.             goLeft = false;
    33.         }else{
    34.             goLeft = true;
    35.             curSpeed = Random.Range(minSpeed, maxSpeed);
    36.         }
    37.        
    38.         Invoke("SwitchMove", delay);
    39.     }
    40. }
     
  20. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    have you set minSpeed, maxSpeed and delay in the inspector?

    If you've done that change the seed line to: Random.seed = System.Environment.TickCount;
     
  21. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    I did that and it stays still
     
  22. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    It's fixed! I hadn't set a delay derp
     
  23. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Just remove the seed line then. Also just noticed the vector.right and vector.left are the wrong way round, this wont afect anything it will jsut make the code make more sense.
     
    GreenBoxInteractive likes this.
  24. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Ah XD. Always the simple things
     
    GreenBoxInteractive likes this.
  25. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Thanks everyone!