I'm struggling how to fix this error. Not all paths return a value. Any Help appreciated

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnManager : MonoBehaviour
{
    public float SpawnRangeX = 9.7f;
    public float SpawnPosY = 4f;
    public GameObject[] EnemyPrefabs;
    public float SpawnDelay = Random.Range(0, 1);
    public float SpawnVariable = 0f;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        while (SpawnVariable == 0)
        {
            SpawnEnemies();
        }
       
        function SpawnEnemies()
        { 
            int EnemyIndex = Random.Range(0, EnemyPrefabs.Length);
            Vector3 SpawnPos = new Vector3(Random.Range(-SpawnRangeX, SpawnRangeX), 0, SpawnPosY);
            Instantiate(EnemyPrefabs[EnemyIndex], SpawnPos, EnemyPrefabs[EnemyIndex].transform.rotation);

        }
    }
}

Either you are doing something I’ve never seen before or its wrong but you have your function inside your update function?

Move it outside your update

void Update()
    {
        while (SpawnVariable == 0)
        {
            SpawnEnemies();
        }
    }

    private void SpawnEnemies()
    {
        int EnemyIndex = Random.Range(0, EnemyPrefabs.Length);
        Vector3 SpawnPos = new Vector3(Random.Range(-SpawnRangeX, SpawnRangeX), 0, SpawnPosY);
        Instantiate(EnemyPrefabs[EnemyIndex], SpawnPos, EnemyPrefabs[EnemyIndex].transform.rotation);
    }

Additionally I don’t see where you are changing SpawnVariable so this would create an infinite loop unless you are changing it from another script? Its also frame rate dependant so if someone was running the game at 120fps is would spawn 120 enemies per second and 30spf 30 enemies etc…, I’m not sure that is your intent or not?

Hello!

Your error does not come from the code you are giving. This error happens when you make a function that require a return value.

For example, if you do a if sentence and put the return inside the if, when the code does not enter the if it can not return anything. So, as error says, “not all path return a value”. So Unity is detecting this even before running the code.

Post the complete error description. It will in wich function is the problem.

Bye!!