- Home /
Duplicate Question
"NullReferenceException: Object reference not set to an instance of an object" Error
I am getting a null reference exception error in my multiplayer FPS, when I am trying to send the character to a spawnpoint after pressing "spawn."
The code starts like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MultiplayerManager : MonoBehaviour
{
Then it calls the spawnpoints:
public bool MatchLoaded;
public MPPlayer MyPlayer;
public GameObject[] Spawnpoint = new GameObject[4];
void Start()
{
instance = this;
PlayerName = PlayerPrefs.GetString("PlayerName");
CurrentMap = MapList[0];
DontDestroyOnLoad(gameObject);
}
Then the server and client spawning systems:
[RPC]
void Server_SpawnPlayer(NetworkPlayer player)
{
int numberspawn = Random.Range(0, Spawnpoint.Length - 1);
networkView.RPC("Client_SpawnPlayer", RPCMode.All, player, Spawnpoint[numberspawn].transform.position + new Vector3(0, 2, 0), Spawnpoint[numberspawn].transform.rotation);
}
[RPC]
void Client_SpawnPlayer(NetworkPlayer player, Vector3 position, Quaternion rotation)
{
MultiplayerManager.GetMPPlayer(player).PlayerIsAlive = true;
MultiplayerManager.GetMPPlayer(player).PlayerHealth = 100;
if (player == MyPlayer.PlayerNetwork)
{
MyPlayer.PlayerManager.ControllerTransform.position = position;
MyPlayer.PlayerManager.ControllerTransform.rotation = rotation;
MyPlayer.PlayerManager.networkView.RPC("Client_PlayerAlive", RPCMode.All);
}
else
{
}
}
and the actual button:
void SpawnMenu()
{
if(GUI.Button(new Rect(5, Screen.height - 40, 250, 35), "Spawn"))
{
if (Network.isServer)
Server_SpawnPlayer(Network.player);
else
networkView.RPC("Server_SpawnPlayer", RPCMode.Server, Network.player);
}
}
Thank you in advance for any and all help! This is a very frustrating issue. If you need more code (for reference) then ask and I will try to get back to you.
Can you post the error message? What line is generating the error?
Sounds like he is instaniating a character to a spot that hasn't been created yet
There are lots of questions with answers telling you how to solve this.
Follow this Question
Related Questions
Keep KGetting this error please help I dont know what to do 1 Answer
Button is not a member of GUI? 2 Answers
Spawning 2 players when the game starts 2 Answers
NullReferenceException 1 Answer