Im having problems with InvokeRepeating

public class Asteroids : MonoBehaviour
{
private Rigidbody2D rb;
public float speed;
public GameObject asteroid;
public float MaxX = 4;
public float MinX = -4;

void Start()
{
    rb = asteroid.GetComponent<Rigidbody2D>();
    InvokeRepeating("SpawnAsteroid", 1, repeatRate: 3);
}

private void FixedUpdate()
{
    rb.velocity = new Vector2(0, speed);
    //Invoke("SpawnAsteroid", 2f);
}

private void SpawnAsteroid()
{
    float RandomX = Random.Range(MinX, MaxX);

    Vector3 randomSpawnPos = new Vector3(RandomX, 30, 0);
    Instantiate(asteroid, randomSpawnPos, Quaternion.identity);
    Debug.Log("New Asteroid");
}

}

I’m very new to c# , but basically it spawns multiple asteroids instead of one and I don’t understand why it doesn’t work
Thanks

InvokeRepeating(“SpawnAsteroid”, 1, 3);

Check this it will work.

InvokeRepeating(“SpawnAsteroid”, 1, 3);

it didnt work

you can maybe use a for loop like :

public class Asteroids : MonoBehaviour 
{ 
     private Rigidbody2D rb; 
     public float speed; 
     public GameObject asteroid; 
     public float MaxX = 4; 
     public float MinX = -4;

     void Start()
     {
         rb = asteroid.GetComponent<Rigidbody2D>();
         for (int i = 0; i < 3; i++)
         {
              SpawnAsteroid();
         }
     }
     private void FixedUpdate()
     {
         rb.velocity = new Vector2(0, speed);
         //Invoke("SpawnAsteroid", 2f);
     }
     private void SpawnAsteroid()
     {
         float RandomX = Random.Range(MinX, MaxX);
 
         Vector3 randomSpawnPos = new Vector3(RandomX, 30, 0);
         Instantiate(asteroid, randomSpawnPos, Quaternion.identity);
         Debug.Log("New Asteroid");
     }
}

You might want to consider using an IENumerator instead. It’s probably a good idea to also set the parent of the created asteroids to something like the spawner script you have. Here’s an example with your use case:

public class AsteroidSpawner : MonoBehaviour {

    [SerializeField] GameObject asteroid;
    [SerializeField] float timeInterval;
    [SerializeField] float MaxX = 4, MinX = -4;

    bool isSpawningAsteroids;
    Coroutine timedAsteroidSpawn;

    private void Start()
    {
        //start the coroutine
        timedAsteroidSpawn = StartCoroutine(SpawnAsteroidTimed());
        //you can stop the coroutine by setting the isSpawningAsteroids to false
        //or by calling StopCoroutine(timedAsteroidSpawn);
    }

    IEnumerator SpawnAsteroidTimed()
    {
        isSpawningAsteroids = true;

        //repeat the loop as long as you want to keep spawning asteroids
        while (isSpawningAsteroids)
        {
            //Call the spawn asteroid
            SpawnAsteroid();
            //wait for as many seconds as the timeInterval variable states
            yield return new WaitForSeconds(timeInterval);
        }
    }

    void SpawnAsteroid()
    {
        float RandomX = Random.Range(MinX, MaxX);
        Vector3 randomSpawnPos = new Vector3(RandomX, 30, 0);
        //instantiate the asteroid and set its parent to this spawner object
        Instantiate(asteroid, randomSpawnPos, Quaternion.identity).transform.SetParent(transform);
    }
}