Unity freezes completely while trying to instantiate after going to next scene

So I’m making a local multiplayer game and I have a character select scene which players join with unity’s build in Player Input Manager, this is on an empty gameObject which also stores a DDOL Singleton script which keeps track of the amount of players. I have a button and when it’s pressed you go to the next scene, and here is where the issue comes in.
I wanted that for the amount of players in the previous scene, spawn these prefabs in that amount, the script can properly find the prefabs, spawnlocations and the singleton script. And nothing goes wrong until I add the Instantiate part to the script, with Debug.Logs it only does it the amount of times it’s supposed to, not an infinite loop or anything. But before even showing the next scene my whole unity freezes, i have to go into task manager and close it that way. And when i comment out the instantiate it works fine, does anyone know where this could go wrong?

using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;


public class playerManager : MonoBehaviour
{
    public List<GameObject> playersList;
    public GameObject Winscreen;
    public GameObject PlayerPrefab;
    public GameObject PlayerSpawnpoint;
    public TextMeshProUGUI text;

    // Start is called before the first frame update
    void Start()
    {
        Winscreen.SetActive(false);


        for (int i = 0; i < SpawnManager.Instance.totalPlayers; i++)
        {
            Debug.Log(PlayerPrefab);
            Debug.Log("test");
            Instantiate(PlayerPrefab, PlayerSpawnpoint.transform.position, Quaternion.identity);
        }
    }

    // Method to remove a player from the list
    public void removePlayer(GameObject player)
    {
        playersList.Remove(player); // Remove the player from the list
    }

    // Update is called once per frame
    void Update()
    {
        if(playersList.Count == 1)
        {
            Time.timeScale = 0f;
            GameObject player = playersList[0];

            Winscreen.SetActive(true);
            text.text = player.name + " has won!";
        }

        foreach (PlayerInput player in SpawnManager.Instance.players)
        {
            //playersList.Add(player.gameObject);
        }
    }
}

Infinite loops in Unity produce no output.

I’m gonna guess your spawn mangler is perhaps endlessly extending the .totalPlayers as you spawn them, assuming they contain a “register” kinda mechanism.

Prove me wrong! Make a real copy (not just a reference!!!) of the total players and freeze it in time, then iterate that list copy instead instead.

Unity will lock up 100% of the time EVERY millisecond your scripting code is running.

Nothing will render, no input will be processed, no Debug.Log() will come out, no GameObjects or transforms will appear to update.

Absolutely NOTHING will happen… until your code either:

  • returns from whatever function it is running

  • yields from whatever coroutine it is running

As long as your code is looping, Unity isn’t going to do even a single frame of change. Nothing.

No exceptions.

“Yield early, yield often, yield like your game depends on it… it does!” - Kurt Dekker

I’m sorry if i understood this wrong but totalPlayers isn’t a list, it’s an interger that adds 1 (totalplayers++) every time a player joins in the first scene. and to add something (not sure if it was in the first post) when instead of using the totalplayers part i use a 1 or 2 it doesn’t freeze/crash my unity. so then it would be:

for (int i = 0; i < 1; i++)
        {
            Debug.Log(PlayerPrefab);
            Debug.Log("test");
            Instantiate(PlayerPrefab, PlayerSpawnpoint.transform.position, Quaternion.identity);
        }

I assume it is a C# property that returns how many players there are. You are using it to count up how many players to add, which (assuming spawning a player will interact with the spawn manager and increase the count) will endlessly increase the count…?

Go inspect spawn manager… the point being, if you’re increasing something in a loop, and that something is used to terminate the loop, it won’t terminate!

yeah so this was it! I thought it was a bug that it said 2 players, i totally didn’t realise it added another one to the interger! thank you so much!