-1

So here is the code, right now i only spawn each frame x Prefabs (Its a simple Game im new to unity so the player is a ball and dodges stuff etc. So i have an amount of prefabs (Floors) that is spawning in front of the palyer)


    public static int numSpawned = 0;
    //Creates an Array to store PreFabs into it
    public static GameObject[] thePath;
    //Sets the x value of the spawning postion to 100
    public int SpawningDistance = 100;
    
    public int numToSpawn = 6;

//Calls the PreFabs Folder
    private void Start()
    {
        thePath = Resources.LoadAll<GameObject>("PreFabs");
    }
    //Sets the Spawn Position and ++ the numSpawned
    void SpawnRandomObject()
    {
        int withItem = Random.Range(0,6);

        GameObject myObj = Instantiate(thePath[withItem]) as GameObject;

        numSpawned++;

        myObj.transform.position = transform.position + new Vector3(0, 0, 0);
    }

    //Til now it Spawns every Frame so thats what you have to fix
    void Update()
    {   //compares the Values
        if (numToSpawn > numSpawned)
        {
            //where your object spawns from
            transform.position = new Vector3(SpawningDistance, 0, 0);
            SpawnRandomObject();

            SpawningDistance = SpawningDistance + 100;
        }
    }

Til now its the problem that i dont know how to controlle the spawning amount and speed so i need help for that. All Prefabs are beeing stored in an array and then spawned randomly out of it.

Arror404
  • 21
  • 3
  • Keep an array of spawned objects and check the count. Also, if you want them released on a cadence, use coroutines mixed with WaitForSeconds. – Captain Skyhawk Jul 22 '20 at 19:21

1 Answers1

0

Instead of using method SpawnRandomObject() every frame, you could create coroutine and yield WaitForSeconds(value).

[SerializeField] private float delaySpawnInSeconds = 0.5f;

...

private void Start()
{ 
    thePath = Resources.LoadAll<GameObject>("PreFabs");
    StartCoroutine(SpawnFloors());
}

Our coroutine:

private IEnumerator SpawnFloors()
{
    while (numToSpawn > numSpawned)
    {
       //SpawnRandomObject();  //if U want to spawn object then wait
       yield return new WaitForSeconds(delaySpawnInSeconds);
       SpawnRandomObject();  //if U want to wait then spawn object
    }
}

Doc: https://docs.unity3d.com/Manual/Coroutines.html

To get spawning amount, you could use Captain Skyhawk's idea to:

Keep an array of spawned objects and check the count.

Arrays: Doc: https://docs.unity3d.com/2018.4/Documentation/ScriptReference/Array-length.html

You can also use lists, for example:

private List<GameObject> exampleList = new List<GameObject>();
private int sizeOfExampleList;

...\\Add items to the list in the code.\\...

private void exampleMethod () 
{
    sizeOfExampleList = exampleList.Count;
}

...

I hope I helped You! :-)

Prar
  • 1
  • 3
  • So the problem is, the solution waits like 1 sec (doesnt matter with value i put into "delaySpawnInSeconds") then it starts to spawn like always with no break inbetween the spawns. And this only work with the bottom "SpawnRandomObject()" with the top part it spawns the same way but without the 1 sec delay. – Arror404 Jul 23 '20 at 14:31
  • 1
    You are right, because I used the SpawnRandomObject() method call in the Update method (silly me!), so SpawnRandomObject() has been called every frame! I edited my answer and now everything should work. Furthermore, in my personal experience, its better to use [SerializeField] rather than public for UnityEditor variables (public variable can be changed by another script by mistake, when [SerializeField] only by UnityEditor). I don't think you need static variables as well. – Prar Jul 23 '20 at 21:29
  • Will be tonight home and check it then, gonna tell you if it worked or not :D. – Arror404 Jul 25 '20 at 11:44
  • @Arror404 If you think my answer helped you, You can upvote and mark it as answer to your question, so other people could see the correct answer. – Prar Aug 10 '20 at 09:23