Search Unity

Creating multiple single instances of a prefab

Discussion in 'Prefabs' started by ShakyHanded, Aug 3, 2020.

  1. ShakyHanded

    ShakyHanded

    Joined:
    Aug 3, 2020
    Posts:
    9
    I'm pretty new to Unity and I'm trying to make a simple rhythm game where you press the arrow keys in time with the notes that pop up on screen. (Project Diva style if that makes any sense.) So I have some code that spawns a prefab for the arrow sprite I want, but because it's in the Update function it infinitely spawns the sprites, but if I put the spawn if statement in another function nothing spawns at all. So I was wondering if there was a way to spawn a single instance of a prefab at at time at varying intervals based on when I need the notes to appear? Or if I'm just going about this all wrong to begin with. Thanks.
    Here's the code I have:
    Code (CSharp):
    1. public class TutorialGameManager : MonoBehaviour
    2. {
    3.  
    4.     public AudioSource theMusic;
    5.  
    6.     public bool isPlaying = false;
    7.  
    8.     public float period = 0.0f;
    9.  
    10.     public float score = 0.0f;
    11.  
    12.  
    13.     public static TutorialGameManager instance;
    14.    
    15.     public GameObject downArrow;
    16.     public GameObject upArrow;
    17.     public GameObject leftArrow;
    18.     public GameObject rightArrow;
    19.    
    20.     public Transform location;
    21.  
    22.  
    23.  
    24.     // Start is called before the first frame update
    25.     void Start()
    26.     {
    27.     instance = this;
    28.  
    29.         theMusic.Play();
    30.     isPlaying = true;
    31.    
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.  
    38.  
    39.  
    40.     if (period >= 3)
    41.     {
    42.         Debug.Log ("Made one");
    43.         SpawnDown();
    44.     }  
    45.    
    46.  
    47.     period += UnityEngine.Time.deltaTime;
    48.        
    49.     }
    50.  
    51.  
    52.  
    53.     void SpawnDown ()
    54.     {
    55.  
    56.         Instantiate (downArrow, location);
    57.  
    58.     }
    59. }
     
  2. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    187
    From your code, it looks like it should spawn one instance at 3 seconds, then never again (because you don't reset period back to 0).
    Do you see "Made One" in the Console?
     
  3. ShakyHanded

    ShakyHanded

    Joined:
    Aug 3, 2020
    Posts:
    9
    It should, and it does put "Made One" in the console, but it doesn't spawn just one, it continuously spawns them. And I think thats because I put >= instead of ==, but when I use == nothing spawns at all and no message comes up.
     
  4. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    187
    It needs to be >= 3 or it will most likely never fire (because period only increases once per frame, so will be something 3.0333 instead of 3.00000).
    All you need to do is add "period = 0;" after your call to SpawnDown():

    Code (CSharp):
    1.     if (period >= 3)
    2.     {
    3.         Debug.Log ("Made one");
    4.         SpawnDown();
    5.         period = 0;
    6.     }  
     
  5. ShakyHanded

    ShakyHanded

    Joined:
    Aug 3, 2020
    Posts:
    9
    ok, thanks!
     
  6. ShakyHanded

    ShakyHanded

    Joined:
    Aug 3, 2020
    Posts:
    9
    Wait, so that would work, except I need to use period to keep track of how far along in the song i am and spawning notes based on that. I don't want to spawn a note automatically every three seconds. Which is why I'm having the problem in the first place. Is there an alternative way to achieve the same results?
     
  7. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    187
    Then I guess I don't understand when exactly you do want to spawn a note. If it's not at a timed interval, what should trigger it?
     
  8. ShakyHanded

    ShakyHanded

    Joined:
    Aug 3, 2020
    Posts:
    9
    What how far along in the song you are. Also, I put in the returning position to 0 to try and test something else but it still spawned infinately.
     
  9. ShakyHanded

    ShakyHanded

    Joined:
    Aug 3, 2020
    Posts:
    9
    If there's no other way to do it I could probably set up timed colliders that move and trigger that way, but it just seems like there should be a simpler solution.
     
  10. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    187
    But isn't that still just "time"? If the notes shouldn't spawn every n seconds in the song, can you give an example of how frequently they should spawn? There are lots of ways to trigger events, it's just not clear when you want to do that.
     
  11. ShakyHanded

    ShakyHanded

    Joined:
    Aug 3, 2020
    Posts:
    9
    I guess what I mean is that I want the notes to spawn at an inconsistent frequency, following the melody not the beat.
     
  12. ShakyHanded

    ShakyHanded

    Joined:
    Aug 3, 2020
    Posts:
    9
    nvm, I'm trying my other method.