Creating multiple single instances of a prefab

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:

public class TutorialGameManager : MonoBehaviour
{

    public AudioSource theMusic;

    public bool isPlaying = false;

    public float period = 0.0f;

    public float score = 0.0f;


    public static TutorialGameManager instance;
   
    public GameObject downArrow;
    public GameObject upArrow;
    public GameObject leftArrow;
    public GameObject rightArrow;
   
    public Transform location;



    // Start is called before the first frame update
    void Start()
    {
    instance = this;

        theMusic.Play();
    isPlaying = true;
   
    }

    // Update is called once per frame
    void Update()
    {



    if (period >= 3)
    {
        Debug.Log ("Made one");
        SpawnDown();
    }   
   

    period += UnityEngine.Time.deltaTime;
       
    }



    void SpawnDown ()
    {

        Instantiate (downArrow, location);

    }
}

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?

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.

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():

    if (period >= 3)
    {
        Debug.Log ("Made one");
        SpawnDown();
        period = 0;
    }

ok, thanks!

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?

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?

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.

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.

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.

I guess what I mean is that I want the notes to spawn at an inconsistent frequency, following the melody not the beat.

nvm, I’m trying my other method.