Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Spawning indefinitely :(

Discussion in 'Scripting' started by Eddef, Jul 15, 2015.

  1. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Hello, this is my script, and it spawns the Spheres indefinitely after these 5 seconds pass... :(
    THE TIMER was working until I ballsed it up, and now the timer stops at 0,02 seconds. I'm a complete beginner... :D

    #pragma strict

    var Sphere : GameObject;
    var spawn_position;
    var timer = 0.0;

    var SpawnCounter : int = 0;
    var SpawnMax : int = 1;

    function spawnSphere ()
    {

    var randomNumber = 1;
    spawn_position = Vector2(0,0);
    var tempSpawnSphere = Instantiate(Sphere, spawn_position, Quaternion.identity);
    this.SpawnCounter++;


    }


    function Start () {
    timer += Time.deltaTime;
    if (timer > 5 && this.SpawnCounter <= this.SpawnMax)
    {
    spawnSphere ();
    timer = 0.0;
    }
    }
     
  2. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Firstly, please use code tags.

    Secondly, you probably want to call it in Update rather than Start. Start will only execute once whilst Update will repeatedly be called :)
     
  3. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Okay, thank you, and how to fix the timer? It's stuck at 0,02sec and no longer spawns anything :/ I messed it
     
  4. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    As I said: change
    Code (csharp):
    1. function Start () {
    to
    Code (csharp):
    1. function Update () {
    And it should work.
     
  5. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Man, I'm soo bad at this :D :D the spawning doesn't happen now
     
  6. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    This is how it looks now... :/

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var Sphere : GameObject;
    5. var spawn_position;
    6. var timer = 0.0;
    7.  
    8. var SpawnCounter : int = 0;
    9. var SpawnMax : int = 1;
    10.  
    11.  
    12. function Update () {
    13. timer += Time.deltaTime;
    14. }
    15. function Start () {
    16.  
    17. if (timer > 5 && this.SpawnCounter <= this.SpawnMax)
    18. {
    19.     spawnSphere ();
    20.     timer = 0.0;
    21. }
    22.  
    23. }
    24.  
    25. function spawnSphere ()
    26. {
    27.  
    28. var randomNumber = 1;
    29.     spawn_position = Vector3(0,0,0);
    30.     var tempSpawnSphere = Instantiate(Sphere, spawn_position, Quaternion.identity);
    31.     this.SpawnCounter++;
    32.  
    33.  
    34. }
    35.  
    36.  
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Is it spawning onto a plane or something? If so, pause it after 6 seconds or so & check the inspector in case it is spawning under the plane or somewhere else in the scene.

    Take the instantiate stuff out of Start.& put it into Update. Start is called at the start & then isn't called again
     
    Jamster likes this.
  8. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var Sphere : GameObject;
    5. var spawn_position;
    6. var timer = 0.0;
    7.  
    8. var SpawnCounter : int = 0;
    9. var SpawnMax : int = 1;
    10.  
    11.  
    12. function Update () {
    13.     timer += Time.deltaTime;
    14.  
    15.     if (timer > 5 && this.SpawnCounter <= this.SpawnMax)
    16.     {
    17.         spawnSphere ();
    18.         timer = 0.0;
    19.     }
    20. }
    21.  
    22. function spawnSphere ()
    23. {
    24.  
    25.     var randomNumber = 1;
    26.     spawn_position = Vector3(0,0,0);
    27.     var tempSpawnSphere = Instantiate(Sphere, spawn_position, Quaternion.identity);
    28.     this.SpawnCounter++;
    29.  
    30.  
    31. }
    I meant the whole of the function not just the top line ;)
     
  9. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Nope, it's spawning in a 2D world
     
  10. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    For some reason it doesn't work, when I put the stuff out the start funcion, and where the start function should be anyway??

    Code (csharp):
    1.  
    2.  
    3. #pragma strict
    4.  
    5. var Sphere : GameObject;
    6. var spawn_position;
    7. var timer = 0.0;
    8.  
    9. var SpawnCounter : int = 0;
    10. var SpawnMax : int = 1;
    11.  
    12.  
    13. function Update () {
    14.     timer += Time.deltaTime;
    15.  
    16.     if (timer > 5 && this.SpawnCounter <= this.SpawnMax)
    17.     {
    18.         spawnSphere ();
    19.         timer = 0.0;
    20.     }
    21.    
    22.     function spawnSphere ()
    23. {
    24.  
    25.     var randomNumber = 1;
    26.     spawn_position = Vector3(0,0,0);
    27.     var tempSpawnSphere = Instantiate(Sphere, spawn_position, Quaternion.identity);
    28.     this.SpawnCounter++;
    29.  
    30.  
    31. }
    32. }
    33.  
    34.  
     
  11. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Copy my code in... You don't need to put the other function in Update... You also don't need a Start function at all...
     
  12. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Where the start function should be?
     
  13. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    ohhhh okay i'll try
     
  14. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Your code spawns a bunch of spheres too :(
     
  15. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Is that not what it's meant to do? :p

    The code I gave you will spawn 2 spheres with 5 seconds delay as your code was trying to in the beginning...?
     
  16. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Well, now it spawns this many (and basically indefinitely) :D
     

    Attached Files:

  17. julienkay

    julienkay

    Joined:
    Nov 12, 2013
    Posts:
    159
    This is just a guess, but did you attach a prefab to the sphere gameobject in your script which contains the script itself? This would cause the spawning to happen recursively.
     
  18. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Nope, I haven't attached the prefab :/
     
  19. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    any ideas? :(
     
  20. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Your script is definitely attached to the Sphere GameObject itself. You can tell it's recursive because of the (clone)(clone)(clone)(clone)(clone)(clone).

    Put it on its own separate GameObject, something like a SphereGenerator prefab.
     
  21. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    So, I need a new prefab only for the script?
     
  22. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Yes.

    Here's the problem:
    - You create a sphere game object with the script that generates more spheres on it.
    - That sphere game object creates a new sphere after 5 seconds.
    - Now you have a scene with two sphere game objects.
    - After 5 seconds, both those spheres create two more spheres.
    - Now you have four sphere game objects.

    It's going to grow exponentially when a game object has a script that generates a copy of itself.
     
  23. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Wow, yeah, that's what I was going for :D
     
  24. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    And wow, IT WORKS!!!!!!!! You're the best, dude :D I might need help later, if you would like to help me with my project in the future, add me on skype - eddefmusic, or facebook - eddef
     
  25. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Now I need to make the spawned sphere go to the left, I have the velocity script, but I can't seem to snap the new prefab to it :(
     
  26. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Add the script to your prefab, that way as each sphere is spawned it will have the script & start moving
     
  27. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Yeah, I did so, but...
     

    Attached Files:

    • yt.jpg
      yt.jpg
      File size:
      622.8 KB
      Views:
      658
  28. julienkay

    julienkay

    Joined:
    Nov 12, 2013
    Posts:
    159
    The property of your velocity script needs a reference to a RigidBody, so you have to attach a RigidBody component to your sphere.
     
  29. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    That's my script right now

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Velocity : MonoBehaviour {
    6.  
    7.     public float moveSpeed = 10;
    8.     public Rigidbody2D rb;
    9.  
    10.     void Start() {
    11.         rb = GetComponent<Rigidbody2D>();
    12.     }
    13.  
    14.     void FixedUpdate() {
    15.  
    16.             rb.velocity = new Vector2(-10, 0);
    17.        
    18.     }
    19.  
    20. }
    21.  
    22.  
     
  30. julienkay

    julienkay

    Joined:
    Nov 12, 2013
    Posts:
    159
    You're using RigidBody2D. You should use the 3D version instead: RigidBody.
     
  31. julienkay

    julienkay

    Joined:
    Nov 12, 2013
    Posts:
    159
    Except you want to create a 2D game. In that case it would be more efficient, to use a RigidBody2D. But make sure you're also using a CircleCollider for your sphere then.
     
  32. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Changed it to RigidBody, but still, the Sphere(1) doesn't snap to Rb, so it doesn't snap with the velocity script and thus - is not moving :(
     
  33. julienkay

    julienkay

    Joined:
    Nov 12, 2013
    Posts:
    159
    In your screenshot earlier there is no RigidBody component attached to the sphere. Did you fix that? You can add one by clicking the "Add Component" button in the Inspector when the sphere is selected and then choosing "Physics -> RigidBody". This RigidBody component needs to be assigned to the Rb property of the velocity script.
     
  34. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Yes it works great now! Thank you! Now I would like to do, that the Sphere spawning time got faster and faster somehow, and that it started spawning more and more spheres after a while, is that hard to do? :(
     
  35. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Just work out what exactly you want it to do & then step through it, making a note of each item you need e.g. Will you keep a record of what the timer was so you can deduct a set time from it when you reset the timer after the sphere is instantiated? Or do you want the timer to be the same each time but the number of spheres spawned increases by 1 each time?
     
  36. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Would be better that the timer was shorter and shorter, by like 0.25 seconds every Sphere spawns, and when the timer reaches, let's say, 4 seconds, it starts spawning 2 spheres, and when the timer reaches 3 seconds - 3 spheres and ext. ;)
     
  37. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    How to make the Sphere disappear after it touches the white box?
     
  38. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    How to make the Sphere disappear after it touches the white box?
     
  39. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    When it collides, check what the sphere is colliding with & if it is the white box then destroy the sphere.

    For the timer, have 2 floats. One is the current timer & one is the previous timer. When you set the timer set the previous timer to that for the first time. Then each time the current timer reaches zero do something like:
    Current timer = previous timer -0.25f;
    Previous timer = current timer;

    For the sphere spawn, inside the same chunk that resets the timer, do something like:
    If (current timer == 4 || current timer == 3)
    SpawnMax++;
     
  40. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    How would that translate to javascript or C#? :D
    Code (csharp):
    1.  
    2. if (Sphere touches the white block)
    3.          Destroy it;        
    4.  
    5.  
     
  41. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Decide if you are using on collision or on trigger.
    If you use on trigger & put the script on the cube it would be something like:

    C#

    Void OnTriggerEnter(Collider other){
    If (other.gameobject.tag == "sphere")
    Destroy(other.gameobject);
    }


    Note I don't have PC access so there may be an error in this.

    I also suggest doing the tutorials, they will cover things like detecting collisions & destroying stuff & they will explain how it works. You will learn more that way.
     
  42. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    I'm using Unity 5.1.1, and it doesn't work... :( Could you help me to get it working, please? If you would accept, add me on skype eddefmusic, it would be easier to chat there
     
  43. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You need to create a tag called sphere & assign it to your sphere prefab to get that to work, otherwise create a tag called cube or something, assign it to the white cube & put the check onto the sphere script (if u do that then just delete the gameobject, not the other.gameobject.

    I can't Skype as I'm on iPad at a hotel & the Internet is expensive. I can't stress how useful it will be if you put your project away for a while & do the tutorials. You will learn more & also learn why things are done the way they are. Just asking for help with each script aspect one at a time isn't going to help you with learning.
     
  44. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    Okay, I've added the tag, but I'm recieving some silly syntax errors
    Line 10: "}" expected (CS1513) (Assembly-CSharp)
    NOTE: "I'VE ADDED THE '}' to line 11 but, is it supposed to be there??
    Line 21: Type or namespace definition, or end-of-file expected (CS1022) (Assembly-CSharp)


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Destroy : MonoBehaviour {
    5.  
    6.     void Start () {
    7.  
    8.     }
    9.  
    10.     void Update () {
    11.     }
    12.  
    13.         Void OnTriggerEnter(Collider other)
    14.         {
    15.             If (other.gameobject.tag == "Sphere");
    16.                 Destroy(other.gameobject);
    17.  
    18.         }
    19.  
    20.     }
    21. }
    22.  
     
  45. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I can see the error, it isn't a hard one. Step through the code, the message is indicating a problem with syntax so start by checking for semi colons, then check all the curly brackets match up in pairs, then same for normal brackets.
     
  46. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    I've fixed the errors, but the script doesn't work, and when the "Sphere" touches the "Player" nothing happens, so I added the OnTriggerEnter (); in function update :D BUT

    ERROR LINE 11: No overload for method 'OnTriggerEnter' takes '0' arguments (CS1501) (Assembly-CSharp)
    WARNING LINE 16: Possible mistaken empty statement (CS0642) (Assembly-CSharp)

    But I have my tag set to "Sphere"


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Destroy : MonoBehaviour {
    5.  
    6.     void Start () {
    7.  
    8.     }
    9.  
    10.     void Update () {
    11.         OnTriggerEnter ();
    12.     }
    13.  
    14.         void OnTriggerEnter(Collider other)
    15.         {
    16.             if (other.gameObject.tag == "Sphere");
    17.                 Destroy(other.gameObject);
    18.  
    19.  
    20.         }
    21.  
    22.     }
    23.  
    24.  
     
  47. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    You should remove OnTriggerEnter() from Update() otherwise it will execute that function ever frame, fact is you don't need it at all !

    You will need a collider on both the player and the Sphere to be detected by collisions
    If you don't want objects to pass-thru each other then a rigidbody as well.

    http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

    Okay, I've added the tag, but I'm recieving some silly syntax errors
    Line 10: "}" expected (CS1513) (Assembly-CSharp)
    NOTE: "I'VE ADDED THE '}' to line 11 but, is it supposed to be there??
    Line 21: Type or namespace definition, or end-of-file expected (CS1022) (Assembly-CSharp)

    "public class Destroy : MonoBehaviour "
    The Csharp script in this situation would be called Destroy.cs I would not use that name as that is already being used to call Destroy elsewhere. Call it something like Destroy45 as a class name.
     
    Last edited: Jul 17, 2015
  48. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    @runner that's a horrible class name suggestion, tells you nothing and just has a random number on it. Meaningful names are far more useful. DestroyOnTrigger or something would be far better.

    @Eddef you shouldn't have a semicolon after the if statement, as a beginner get used to using braces {} after an if so you can see that the if is governing a section of code.

    If you are going to have such a class, use variables instead of hardcoded values so you can reuse it in other situations

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class DestroyOnTrigger : MonoBehaviour
    6. {
    7.     public string tagCheck;
    8.  
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         if (other.gameObject.tag == tagCheck)
    12.         {
    13.             Destroy(other.gameObject);
    14.         }
    15.     }
    16. }
    17.  
    you can then add this to anything and setup what kills it in the inspector without having to rewrite the code
     
  49. Eddef

    Eddef

    Joined:
    Jul 15, 2015
    Posts:
    43
    For some reason even your code doesn't work :O it stays at the box and doesn't dissapear
     

    Attached Files:

    • yt.jpg
      yt.jpg
      File size:
      773.7 KB
      Views:
      677
  50. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    LeftyRighty said:
    @runner that's a horrible class name suggestion, tells you nothing and just has a random number on it. Meaningful names are far more useful. DestroyOnTrigger or something would be far better.


    @LeftyRighty Your right the classname is a horrible example to use numbers "Im bad"..