Search Unity

[JS] Trying to spawn a sprite randomly every second

Discussion in 'Scripting' started by slimabob, Aug 20, 2014.

  1. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    Hello unity forums!

    I am currently trying to figure out how to, as the title said, spawn a sprite in a random location every second. At the moment, the timer that is supposed to count the time in between spawns doesn't appear to trigger the spawning function when it reaches 1 second like I need it to.

    Here's the code:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var word : GameObject;
    4. var spawn_position;
    5. var timer = 0.0;
    6.  
    7. function SpawnWord(){
    8. Debug.Log("Spawning");
    9. spawn_position = Vector2(Random.Range(1,800),Random.Range(1,800));
    10. var tempspawnword = Instantiate(word, spawn_position, Quaternion.identity);
    11. }
    12.  
    13. function Start () {
    14.  
    15. }
    16.  
    17. function Update () {
    18. timer += Time.deltaTime;
    19. if(timer >= 1.0)
    20.     {
    21.     SpawnWord();
    22.     timer = 0.0;
    23.     }
    24. }
    Also if I'm doing anything else wrong here that won't work, please let me know, I'm still pretty new to javascript! (Normally I would be asking on the Unity Answers page, but it won't let me create any new questions :/)
     
  2. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Have you tested the function itself? Does it work?

    Edit: There is no need to leave a function Start() if you don't write anything inside.
     
  3. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    Thanks for this, I can't believe I didn't try the function by itself. It did not seem to work, so I ended up re-writing everything and had it work this time.

    Here's the code if anyone ends up finding this and needs something to reference:

    Code (JavaScript):
    1. private var wordClone : GameObject;
    2. var word : GameObject;
    3. var wordX;
    4. var wordY;
    5. private var wordPosition;
    6. var timer = 0.0;
    7.  
    8. function Update () {
    9.     wordX = Random.Range(-5.0, 5.0);
    10.     wordY = Random.Range(-5.0, 5.0);
    11.     wordPosition = Vector3(wordX, wordY, transform.position.z);
    12.  
    13.     timer += Time.deltaTime;
    14.     if(timer >= 1.0)
    15.     {
    16.         Debug.Log("spawning");
    17.         Spawnword();
    18.         timer = 0.0;
    19.     }
    20. }
    21. function Spawnword ()
    22. {
    23.     wordClone = Instantiate(word, wordPosition, transform.rotation);
    24. }
     
    elmar1028 likes this.
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    elmar1028 likes this.
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Eric is right. Use invokeRepeating. It will save you tons of time!
     
  6. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    Thanks, this helps a lot!