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);
}
}
}