- Home /
Question by
Greyeeh · Jan 10, 2016 at 08:59 PM ·
networkinginstantiateconnection
Network.Instantiate missing reference exception
Hello. I'm spawning a player using Network.Instantiate but after disconnecting from the server and connecting/hosting again I'm getting this error "MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.".
I've been trying to remove player's game objects but it didn't help at all. At this point I'm lost and I've got no idea how to fix this issue.
Here's the script I'm using to spawn my player:
using UnityEngine; using System.Collections.Generic;
public class NetworkSpawn : MonoBehaviour
{
public static bool blue = false;
public static bool red = false;
private GameObject[] spawns;
public static List<GameObject> BlueSpawns = new List<GameObject>();
public static List<GameObject> RedSpawns = new List<GameObject>();
private GameObject playerPefab;
private static int blueGroup = 0;
private static int redGroup = 1;
private static int team = -1;
void Start()
{
spawns = GameObject.FindGameObjectsWithTag("Spawn");
playerPefab = Resources.Load<GameObject>("Prefabs/Player");
SetupSpawnPoints();
}
void SetupSpawnPoints()
{
foreach (GameObject spawn in spawns)
{
if (spawn.GetComponent<SpawnPoint>())
{
if (spawn.GetComponent<SpawnPoint>().team == blueGroup)
{
BlueSpawns.Add(spawn);
}
else if (spawn.GetComponent<SpawnPoint>().team == redGroup)
{
RedSpawns.Add(spawn);
}
}
else
{
Debug.Log("Error, selected spawn doesn't have required SpawnPoint script!");
}
}
}
void OnServerInitialized()
{
SetupButtons();
}
void OnConnectedToServer()
{
SetupButtons();
}
public void SetupButtons()
{
UIStartScreen.instance.Enable(false);
UISpectator.Enable(true);
UISpectator.spectatorPanel.GetComponent<UISpectator>().blueButton.onClick.AddListener(JoinBlue);
UISpectator.spectatorPanel.GetComponent<UISpectator>().redButton.onClick.AddListener(JoinRed);
PlayerInputs.inGame = false;
PlayerInputs.inMenu = true;
}
public void JoinBlue()
{
blue = true;
team = 0;
UISpectator.Enable(false);
playerPefab = Resources.Load<GameObject>("Prefabs/Player");
GameObject randomSpawn = BlueSpawns[Random.Range(0, BlueSpawns.Count - 1)];
Network.Instantiate(playerPefab, randomSpawn.transform.position, randomSpawn.transform.localRotation, blueGroup);
UpdateLocalGameManager(team);
PlayerInputs.inGame = true;
PlayerInputs.inMenu = false;
PlayerInputs.escaped = false;
}
public void JoinRed()
{
red = true;
team = 1;
UISpectator.Enable(false);
playerPefab = Resources.Load<GameObject>("Prefabs/Player");
GameObject randomSpawn = RedSpawns[Random.Range(0, RedSpawns.Count - 1)];
Network.Instantiate(playerPefab, randomSpawn.transform.position, randomSpawn.transform.localRotation, redGroup);
UpdateLocalGameManager(team);
PlayerInputs.inGame = true;
PlayerInputs.inMenu = false;
PlayerInputs.escaped = false;
}
static void UpdateLocalGameManager(int team)
{
GameObject gameManager = GameObject.Find("GameManager");
PlayerDatabase database = gameManager.GetComponent<PlayerDatabase>();
database.playerTeam = team;
database.joinedTeam = true;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612055730im_/https://answers.unity.com/themes/thub/images/avi.jpg)