InvokeRepeating Doesn't work properly

I want to spawn an object every 0.5 sec, but when the program starts it spawn everything at the same time.

void Start()
    {
      
        for (int i = 0; i < 20; i++)
        {
            SpawPiattaforma();
        }

    }

    // Update is called once per frame
    void Update()
    {
        if (gameOver)
            CancelInvoke("SpawPiattaforma");
        if (MovimentoPalla.current.partito)
            InvokeRepeating("SpawPiattaforma", 1, 0.5f);
    }

Any ideas? Thank you!

You call another InvokeRepeating every frame. Thus, 60 times pers second you ask unity to start spawning object every 0,5 seconds. Of course it spawning all the time. Call it once.

thanks but how?

That’s depends on your game logic. Find the right moment and invoke it. If you want just test, call it in Start method and see it works.

I’m spawning a platform where the ball is rolling, so I need that It is spawned every x time.

I called it in the start method and it does work but when I start the game it stops, but I need that the platform is spawned continuously

Thanks

You’d better rely on ball’s position than time. If I understood you correct, it’s kinda infinite rolling ball game? Add trigger like this

o____|_

and when ball hits the trigger

____o|_

spawn another platform nd move trigger forward

_____o_  ____|_

This way you can make platform any shape length and give ball any speed and acceleration without reconfiguring timings for every change

What @palex-nx said is really the only reliable way to do it position or distance.

In my game (check sig, its a really crappy game btw.) I base spawning off of distance between to make sure its timed just right, or appears that way as its not based on time. That way I can change the distance and speed on the fly and everything works out(hopefully.)

The camera doesn’t move, so everything is moving from right to left. Distance is like position in this scenario. As the game progresses the speed increases and the distance between “toilets” decreases, increasing difficulty. The distance is random based on the difficulty as it increase with the passing of time.